Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

matthew858

macrumors member
Original poster
Apr 15, 2008
97
0
Sydney, Australia
I have the following timestamp:

Code:
Wed May 05 22:52:51 +0000 2007

which I need to convert into a Unix Timestamp. I have the following code:
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?
 
Your format string is wrong. Try this:
Code:
[df setDateFormat:@"EEE MMM dd HH:mm:ss ZZ yyyy"];

Also, if you want a unix style time value you can use this:
Code:
NSTimeInterval unixInterval = [myDate timeIntervalSince1970];
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.