NSDateFormatter将日期设置为1970年

时间:2011-09-22 18:47:51

标签: ios nstimer nsdateformatter

在我的summarydict中,我有一个完整的date_generated,它看起来像: 9月22日,下午2:33 ,我想将其转换为{{ 1}}以便我以后可以将它送入重复的计时器。

所以这是我的代码:

NSDate

这就是我得到的:

ALog(@"Last update time is: %@", [[summarydict objectForKey:@"root"]objectForKey:@"date_generated"]);
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLenient:YES];
[dateFormatter setDateFormat:@"MM dd, h:mm a"];
[dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease]];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"America/New_York"]];
NSDate *date = [dateFormatter dateFromString:[[summarydict objectForKey:@"root"]objectForKey:@"date_generated"]];
ALog(@"Using timezone: %@", [dateFormatter timeZone]);
ALog(@"Last updated time: %@", date);
ALog(@"Current time: %@", [NSDate dateWithTimeIntervalSinceNow:0]);
ALog(@"Next update time: %@", [date dateByAddingTimeInterval:600]);

我理解Last update time is: Sep 22, 2:33 PM Using timezone: America/New_York (EDT) offset -14400 (Daylight) Last updated time: 1970-09-22 18:33:00 +0000 Current time: 2011-09-22 18:43:05 +0000 Next update time: 1970-09-22 18:43:00 +0000 (我的ALog)不关心timezoon,所以通过将其显示为GMT timezoon很好,但我怎样才能让NSLog认为我拥有的 9月22日下午2:33 是今年的?我有formatter但它不起作用。

谢谢!

1 个答案:

答案 0 :(得分:1)

您如何看待这个:

NSString *str = [NSString stringWithFormat:@"2011 %@", [[summarydict objectForKey:@"root"]objectForKey:@"date_generated"]];
NSLog(@"Last update time is: %@", str);
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLenient:YES];
[dateFormatter setDateFormat:@"yyyy MM dd, h:mm a"];
[dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease]];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"America/New_York"]];
NSDate *date = [dateFormatter dateFromString:str];
NSLog(@"Using timezone: %@", [dateFormatter timeZone]);
NSLog(@"Last updated time: %@", date);
NSLog(@"Current time: %@", [NSDate dateWithTimeIntervalSinceNow:0]);
NSLog(@"Next update time: %@", [date dateByAddingTimeInterval:600]);