Question

I have a string date "Mar+5,+2014+4:00:00+AM"and I tried convert NSDate, and next get long(NSTimeInterval):

expireDate = @"Mar+5,+2014+4:00:00+AM";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"MMM+dd,+yyyy+h:mm:ss+a"];
NSDate *dte = [dateFormat dateFromString:expireDate];

NSLog(@"%f", [dte timeIntervalSince1970]);

But, show me result: 0000.0. What the problem? Please help me. I need this date in long for store in db.

Was it helpful?

Solution

It might be necessary to set a locale, to be independent of the user's settings:

[dateFormat setLocale:[NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"]];

OTHER TIPS

It looks like your code works, possibly change the format to %.0f to only show seconds.
Added an additional NSLog of the date for debugging.

NSString *expireDate = @"Mar+5,+2014+4:00:00+AM";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"MMM+dd,+yyyy+h:mm:ss+a"];
NSDate *dte = [dateFormat dateFromString:expireDate];
NSLog(@"%@", dte);
NSLog(@"%.0f", [dte timeIntervalSince1970]);

NSLog output for my timezone (EST):

2014-03-05 09:00:00 +0000
1394010000

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top