Search code examples
iosxcode4.2nsdateformatter

How to get the correct date by using dateformatter method?


here is my code:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];

NSDate *date = [NSDate dateWithTimeIntervalSinceReferenceDate:1275822000000+0000];

// the string is json parsing string

NSString *formattedDateString = [dateFormatter stringFromDate:date];
NSLog(@"formattedDateString: %@", formattedDateString);

The output is Dec 20,5828963

But my required output is June 6,2010

How to change my code for correct output?


Solution

  • If you want to receive June 6, 2010 from some magic number then use this:

    NSDate *date = [NSDate dateWithTimeIntervalSinceReferenceDate:297475822];
    

    For some reason your number is very big, and as @wrock mentioned it multiplies on 10 million, so for getting correct value you can use dateWithTimeIntervalSince1970 and divide it on 10 million

    NSDate *date = [NSDate dateWithTimeIntervalSince1970:1275822000];