31

For my iPhone application...

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

"created_time": "2010-12-01T21:35:43+0000"

I am also looking for a way to format the Twitter date.

"created_at": "Wed Dec 01 17:08:03 +0000 2010"

I wish to change both to be in the format of Thu Nov 18, 2010 06:48 AM, or something similar. I have had no luck using NSDateFormatter.

How can i format these dates from Facebook and Twitter using Objective-C into the format i desire?

james
  • 26,141
  • 19
  • 95
  • 113

8 Answers8

68

here is what i have working for Twitter:

NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
    //Wed Dec 01 17:08:03 +0000 2010
    [df setDateFormat:@"eee MMM dd HH:mm:ss ZZZZ yyyy"];
    NSDate *date = [df dateFromString:[[tweets objectAtIndex: storyIndex] objectForKey: TWITTER_CREATED_AT_JSON_KEY]];
    [df setDateFormat:@"eee MMM dd yyyy"];
    NSString *dateStr = [df stringFromDate:date];

where tweets is an NSMutableArray filled with NSDictionary objects, storyIndex being the row int value (in the tableview), and TWITTER_CREATED_AT_JSON_KEY being a constant NSString with value created_at. use the dateStr wherever you wish to display the date

and Facebook:

NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
    //2010-12-01T21:35:43+0000  
    [df setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZZZZ"];
    NSDate *date = [df dateFromString:[[facebook objectForKey:FACEBOOK_POST_CREATED_TIME_JSON_KEY] stringByReplacingOccurrencesOfString:@"T" withString:@""]];
    [df setDateFormat:@"eee MMM dd, yyyy hh:mm"];
    NSString *dateStr = [df stringFromDate:date];
Peter DeWeese
  • 18,141
  • 8
  • 79
  • 101
james
  • 26,141
  • 19
  • 95
  • 113
  • 10
    correction for the Facebook format: [df setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZZZZ"]; This format solves two problems - 1. take note of the capital MM (months), instead of mm 2. no need to replace T with empty string when calling dateFromString later – mkto Jul 24 '11 at 17:19
  • 3
    For those who are not using the American or British locale this doesn't work. You have to set the locale of the dateformatter to: [dateForm setLocale: [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]]; – Quxflux Dec 04 '12 at 15:43
  • Another correction: "ZZZZ" outputs "GMT-0000" while "ZZ" gives you just "-0000" – Undeadlegion Oct 07 '13 at 19:30
  • ohh .. Thanks a lot :) – Anurag Bhakuni Jun 14 '15 at 06:08
10

I give you the solution to parse Facebook api date time:

parser = [[NSDateFormatter alloc] init];
[parser setTimeStyle:NSDateFormatterFullStyle];
[parser setFormatterBehavior:NSDateFormatterBehavior10_4];
[parser setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssz"];

Enjoy !

eBuildy, french Web Agency

Thomas Decaux
  • 21,738
  • 2
  • 113
  • 124
4

@binnyb's answer is perfect for Objective-C; for those looking to do the same thing using Swift, here's a translated version of his answer:

For the Twitter Date:

    var df = NSDateFormatter()
    //Wed Dec 01 17:08:03 +0000 2010
    df.dateFormat = "eee MMM dd HH:mm:ss ZZZZ yyyy"
    var date = df.dateFromString(twitterDateString)
    df.dateFormat = "eee MMM dd yyyy"
    var dateStr = df.stringFromDate(date!)

For the Facebook Date:

    var df = NSDateFormatter()
    //Wed Dec 01 17:08:03 +0000 2010
    df.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZZZZ"
    var date = df.dateFromString(facebookDateString)
    df.dateFormat = "eee MMM dd yyyy"
    var dateStr = df.stringFromDate(date!)

where twitterDateString and facebookDateString are the date strings obtained from their respective APIs.

Community
  • 1
  • 1
Ruben Martinez Jr.
  • 3,199
  • 5
  • 42
  • 76
3
"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
}
Nimit Parekh
  • 16,776
  • 8
  • 50
  • 72
0

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
}
Robert Erdei
  • 11
  • 1
  • 4
0

something similar to this should do the first one for you. i would double check against the docs, as i may have capitalised the wrong letters.

[dateFormatter setDateFormat:@"YYYY-MM-ddThh:mm:ss z"];
NSDate *date=[NSDateFormatter dateFromString:twitterDate];
MCannon
  • 4,011
  • 2
  • 16
  • 12
  • this did not work. also i believe what you entered is a little wrong: `NSDateFormatter` should be replaced with `dateFormatter`. when i use this code to format the above Twitter date, i get a null string using `NSString *d = [dateFormatter stringWithDate:date];` – james Dec 02 '10 at 14:26
0
NSString *yourInputFBdate = @"2010-12-02T20:12:06+0000";
NSString *fbDate = [yourInputFBdate stringByReplacingOccurrencesOfString:@"T" withString:@" "];
fbDate = [fbDate stringByReplacingOccurrencesOfString:@"+" withString:@" +"];
NSDate *date = [[NSDate alloc] initWithString:fbDate];

/// do something with date ...

[date release];
cybercow
  • 427
  • 1
  • 4
  • 18
0

Just to complicate things a bit, the JSON response of the Twitter Search API currently returns a date format (inherited from Summize) which is something like:

@"Sun, 05 Jun 2011 12:25:47 +0000"

To convert this to NSDate, use:

NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:@"eee, dd MMM yyyy HH:mm:ss ZZZZ"];
NSDate *date = [dateFormatter dateFromString:@"Sun, 05 Jun 2011 12:25:47 +0000"];
WebSeed
  • 1,687
  • 1
  • 15
  • 19