2

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?

4

3 に答える 3

2

まず、タイムスタンプはJavaタイムスタンプのように見えますが、いくつかの余分なゼロがあります(そのうちの4つは正確に)。これをUnixタイムスタンプに変換する必要があります。これはUnixエポックからの秒数です(Unixエポックからのミリ秒数であるJavaとは対照的です)。

dateWithTimeIntervalSinceReferenceDate:Unixタイムスタンプを受け入れませんが、変換は簡単です。

double timestampFromService = 12758220000000000;
NSDate *date = [NSDate dateWithTimeIntervalSince1970:
    (timestampFromService*1.0e-7)];
于 2012-05-24T12:50:19.497 に答える
2

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];
于 2012-05-24T12:38:18.370 に答える
0

Use date in readable manner :

NSDate *myDate = [NSDate dateWithString:@"2010-06-06 00:00:00"];

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

NSString *formattedDateString = [dateFormatter stringFromDate:myDate];
NSLog(@"formattedDateString: %@", formattedDateString);
于 2012-05-24T12:34:10.423 に答える