How to format Facebook/Twitter dates (from the JSON feed) in Objective-C

前端 未结 8 997
有刺的猬
有刺的猬 2020-12-13 02:39

For my iPhone application...

I am looking for a way to format the Facebook created_time JSON property value.

\"created_time\": \"2010-12-0

8条回答
  •  庸人自扰
    2020-12-13 03:25

    Translated to Swift 2 (Nimit Parekh answer)

    func retrivePostTime(postDate: String) -> String {
        let df: NSDateFormatter = NSDateFormatter()
        df.dateFormat = "eee MMM dd HH:mm:ss ZZZZ yyyy"
    
        let userPostdate: NSDate = df.dateFromString(postDate)!
        let currentDate: NSDate = NSDate()
        let distanceBetweenDates: NSTimeInterval = currentDate.timeIntervalSinceDate(userPostdate)
        let theTimeInterval: NSTimeInterval = distanceBetweenDates
        let sysCalendar: NSCalendar = NSCalendar.currentCalendar()
        let date1: NSDate = NSDate()
        let date2: NSDate = NSDate(timeInterval: theTimeInterval, sinceDate: date1)
        let conversionInfo: NSDateComponents = sysCalendar.components([.Hour, .Minute, .Day, .Month, .Second], fromDate: date1, toDate: date2, options: [])
    
        var returnDate: String = ""
        if conversionInfo.month > 0 { returnDate = String(format: "%ldmth", conversionInfo.month) }
        else if conversionInfo.day > 0 { returnDate = String(format: "%ldd", conversionInfo.day) }
        else if conversionInfo.hour > 0 { returnDate = String(format: "%ldh", conversionInfo.hour) }
        else if conversionInfo.minute > 0 { returnDate = String(format: "%ldm", conversionInfo.minute) }
        else if conversionInfo.second > 0 { returnDate = String(format: "%lds", conversionInfo.second) }
    
        return returnDate
    }
    

提交回复
热议问题