【发布时间】:2012-05-30 09:42:19
【问题描述】:
我有一个代表NSTimeInterval 的双精度值。我想从双精度值创建正确的日期,这是我的代码。我得到了输出,但实际值有一些变化。
NSString *modiDate = [NSString stringWithFormat:@"1338229800"]; //example time interval value
NSNumber *time = [NSNumber numberWithDouble:([modiDate doubleValue] - 3600)];
NSTimeInterval interval = [time doubleValue];
NSDate *online = [NSDate date];
online = [NSDate dateWithTimeIntervalSince1970:interval];
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:@"MM/dd/yyyy HH:mm:ss aaa"];
NSLog(@"result: %@", [dateFormatter stringFromDate:online]);
这里出了什么问题?有什么想法吗?
编辑:
为了首先进行测试,我将日期转换为NSTimeInterval 并尝试取回日期。
这是代码和输出。
NSString *modDate = [NSString stringWithFormat:@"5/30/2012 2:02:55 PM"];
NSDateFormatter *format = [[NSDateFormatter alloc]init];
[format setDateFormat:@"MM/dd/yyyy HH:mm:ss aaa"];
NSDate *dateFromString1 = [[NSDate alloc] init];
dateFromString1 = [format dateFromString:modDate];
[format release];
NSTimeInterval timeInterval1 = [dateFromString1 timeIntervalSince1970];
NSLog(@"timeinterval : %f",timeInterval1);
NSDate *online = [NSDate date];
online = [NSDate dateWithTimeIntervalSince1970:timeInterval1];
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:@"MM/dd/yyyy HH:mm:ss aaa"];
NSLog(@"result: %@", [dateFormatter stringFromDate:online]);
输出是:
result: 05/30/2012 12:02:55 PM actual date:5/30/2012 2:02:55 PM
【问题讨论】:
-
有哪些变化? NSLog 输出在哪里?
-
我已经用代码和结果更新了我的问题
标签: iphone nsdate nsdateformatter nstimeinterval