NSDate formatter issue with Twitter JSON API

2019-04-14 23:50发布

I'm trying to format JSON response date value:

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

I follow instructions of this question:

Date/Time parsing in iOS: how to deal (or not deal) with timezones?

Actually, date value in JSON response is:

created_at":"Mon, 28 May 2012 11:20:29 +0000"

Date variable is nil when receiving value by [df dateFromString:[twitter objectForKey:@"created_at"]];

Many thanks

5条回答
霸刀☆藐视天下
2楼-- · 2019-04-14 23:58

i found that the "created_at" attribute in the twitter Json response is in this format:

Thu Apr 19 10:07:29 +0000 2012

so i used this formatter :

[df setDateFormat:@"EEE MMM d HH:mm:ss Z y"];

it's working fine ;)

查看更多
爷的心禁止访问
3楼-- · 2019-04-15 00:04

Right formatter value is:

[df setDateFormat:@"eee, dd MMM yyyy HH:mm:ss ZZZZ"];

for:

created_at":"Mon, 28 May 2012 11:20:29 +0000"
查看更多
等我变得足够好
4楼-- · 2019-04-15 00:05

To validate the format you're using, create a string from a date:

NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"eee, MMM dd HH:mm:ss ZZZZ yyyy"];
NSLog(@"%@", [df stringFromDate: [NSDate date]];

Compare this with the date from your JSON response and you should be able to see where your format is wrong.


In Swift

let df = NSDateFormatter()
df.dateFormat = "eee, MMM dd HH:mm:ss ZZZZ yyyy"
let dateStr = df.stringFromDate(NSDate())
println("\(dateStr)")
查看更多
趁早两清
5楼-- · 2019-04-15 00:05
"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;
}
查看更多
啃猪蹄的小仙女
6楼-- · 2019-04-15 00:20

try this format if the month is abbreviated (Sept) :

[df setDateFormat:@"EEE, d MMM y HH:mm:ss Z"];;

and

[df setDateFormat:@"EEE, d MMMM y HH:mm:ss Z"];

if the month name is written out (September).

the full list of format specifiers.

I also had to change the locale

NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease]];
[df setDateFormat:@"EEE, d MMM y HH:mm:ss Z"];
NSDate *date = [df dateFromString:@"Mon, 28 May 2012 11:20:29 +0000"];

[df setDateFormat:@"dd/MM/yyyy"];
NSString *dateStr = [df stringFromDate:date];

NSLog(@"%@",dateStr);

output

28/05/2012
查看更多
登录 后发表回答