views:

22

answers:

1

Hi guys, I got a small problem . I have this date format from .net json output:

Date(1280471989140);

when I try to convert using

+ (NSDate*)dateFromUnixDate:(double)unixdate {
    NSTimeInterval unixDate = unixdate;
    return [NSDate dateWithTimeIntervalSince1970:unixDate];}

but I got the wrong date back,why?

thanks

+2  A: 

I just did some checking, JSON Date format is in milliseconds since 1970, not seconds. To make this work you want to change your function to

+ (NSDate*)dateFromUnixDate:(double)unixdate 
{
    NSTimeInterval unixDate = unixdate / 1000.0;
    return [NSDate dateWithTimeIntervalSince1970:unixDate];
}

or without a category method this can work as well

int main(int argc, char *argv[]) 
{
    uint64_t jsonDate = 1280471989140UL;
 NSTimeInterval unixDate = (double)jsonDate / 1000.0;
 NSDate *date = [NSDate dateWithTimeIntervalSince1970:unixDate];
 NSLog(@"%@",date);
}
Joshua Weinberg
GREAT!!! :) thanks 1million
DigitalVanilla
You're welcome, accepting my answer is worth almost 1 million :)
Joshua Weinberg
uau... btw, it seems It doesn works.. I got always 1970-01-01 01:00:00 +0100
DigitalVanilla
show your current code please
Joshua Weinberg
That one was the entire code. If you trim the date and give the double value does it works for you?
DigitalVanilla
Just edited my answer to show my test code, this works fine, and gives the result 2010-07-30 01:38:56 -0500
Joshua Weinberg
Ok,it's working. Thanks again 2million this time :)
DigitalVanilla
NSTimeInterval is a typedef for double. Your cast should be to double, not float.
JeremyP
I also should be adding UL on the end of the first piece, as its truncating. But it gets the point across
Joshua Weinberg