PDA

View Full Version : Help With Timestamp and NSDateFormatter




matthew858
Nov 28, 2009, 04:27 AM
I have the following timestamp:


Wed May 05 22:52:51 +0000 2007


which I need to convert into a Unix Timestamp. I have the following code:

NSString *time = timestamp.text;
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"aaa bbb dd HH:MM:SS zzzz YYYY"];
NSDate *myDate = [df dateFromString:time];
NSTimeInterval unixInterval = [myDate timeIntervalSinceDate:[NSDate dateWithNaturalLanguageString:@"01/01/1970"]];
result.text = [[NSString alloc] initWithFormat:@"%0.0f",unixInterval];


But this returns 0. I feel the issue may lie where I set the date format, but I'm not sure what to replace this with. Any ideas?



bredell
Nov 28, 2009, 09:32 AM
Your format string is wrong. Try this:

[df setDateFormat:@"EEE MMM dd HH:mm:ss ZZ yyyy"];


Also, if you want a unix style time value you can use this:

NSTimeInterval unixInterval = [myDate timeIntervalSince1970];