1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

Help With Timestamp and NSDateFormatter

Discussion in 'iPhone/iPad Programming' started by matthew858, Nov 28, 2009.

  1. macrumors member

    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?
  2. macrumors regular

    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];

Share This Page