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

前端 未结 8 974
有刺的猬
有刺的猬 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
    }
    
    0 讨论(0)
  • 2020-12-13 03:30
    "created_at" = "Sat Apr 05 06:31:57 +0000 2014";
    

    Following Method Return How many time before twit aris.

    -(NSString*)retrivePostTime:(NSString*)postDate{
    
        NSDateFormatter *df = [[NSDateFormatter alloc] init];
       [df setDateFormat:@"eee MMM dd HH:mm:ss ZZZZ yyyy"];
        NSDate *userPostDate = [df dateFromString:postDate];
    
    
        NSDate *currentDate = [NSDate date];
        NSTimeInterval distanceBetweenDates = [currentDate timeIntervalSinceDate:userPostDate];
    
        NSTimeInterval theTimeInterval = distanceBetweenDates;
    
        // Get the system calendar
        NSCalendar *sysCalendar = [NSCalendar currentCalendar];
    
        // Create the NSDates
        NSDate *date1 = [[NSDate alloc] init];
        NSDate *date2 = [[NSDate alloc] initWithTimeInterval:theTimeInterval sinceDate:date1];
    
        // Get conversion to months, days, hours, minutes
        unsigned int unitFlags = NSHourCalendarUnit | NSMinuteCalendarUnit | NSDayCalendarUnit | NSMonthCalendarUnit | NSSecondCalendarUnit;
    
        NSDateComponents *conversionInfo = [sysCalendar components:unitFlags fromDate:date1  toDate:date2  options:0];
    
        NSString *returnDate;
        if ([conversionInfo month] > 0) {
            returnDate = [NSString stringWithFormat:@"%ldmth ago",(long)[conversionInfo month]];
        }else if ([conversionInfo day] > 0){
            returnDate = [NSString stringWithFormat:@"%ldd ago",(long)[conversionInfo day]];
        }else if ([conversionInfo hour]>0){
            returnDate = [NSString stringWithFormat:@"%ldh ago",(long)[conversionInfo hour]];
        }else if ([conversionInfo minute]>0){
            returnDate = [NSString stringWithFormat:@"%ldm ago",(long)[conversionInfo minute]];
        }else{
            returnDate = [NSString stringWithFormat:@"%lds ago",(long)[conversionInfo second]];
        }
        return returnDate;
    }
    

    May this help lot.

    Translated to Swift 2

    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
    }
    
    0 讨论(0)
提交回复
热议问题