Reputation: 919
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
Upvotes: 1
Views: 3426
Reputation: 53141
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)")
Upvotes: 3
Reputation: 16864
"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;
}
Upvotes: 0
Reputation: 943
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 ;)
Upvotes: 4
Reputation: 52227
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
Upvotes: 6
Reputation: 919
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"
Upvotes: 0