I need to convert a date in this string format:
"2011-01-12T14:17:55.043Z"
to a number like 1294841716 (which is the number of seconds [not milliseconds] since Jan. 1st, 1970). Is there an easy way to do this parsing?
Update: Here is the code I've got so far:
NSString *dateString = @"2011-01-12T14:17:55.043Z";
NSDateFormatter *inFormat = [[NSDateFormatter alloc] init];
[inFormat setDateFormat:@"yyyy-MM-ddTHH:mm:ss.nnnZ"];
NSDate *parsed = [inFormat dateFromString:dateString];
long t = [parsed timeIntervalSinceReferenceDate];
But t
comes back as 0 every time.
Use NSDateFormatter to get a NSDate
then use - (NSTimeInterval)timeIntervalSince1970
to get the seconds since 1970.
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];
[formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];
NSDate *date = [formatter dateFromString:@"2011-01-12T14:17:55.043Z"];
NSLog(@"Date: %@", date);
NSLog(@"1970: %f", [date timeIntervalSince1970]);
NSLog(@"sDate: %@", [formatter stringFromDate:date]);
[formatter release];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With