新加坡当地时间(SGT)的NSDate在1933-01-01 00:00:00返回nil

时间:2014-10-16 03:11:25

标签: ios objective-c iphone datetime timezone

如果运行此代码,您可以看到1933-01-01 00:00:00 +0000的NSDate始终为空。请帮我解决一下。

NSString *date = @"1933-01-01 00:00:00 +0000";

 NSString* dateString = [[NSString stringWithFormat:@"%@", date] stringByReplacingOccurrencesOfString:@" +0000" withString:@""];
        NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"SGT"]];
        [dateFormatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
        [dateFormatter setLocale:[NSLocale systemLocale]];
        [dateFormatter setFormatterBehavior:NSDateFormatterBehaviorDefault];
        NSDate* sgtDate = [dateFormatter dateFromString:dateString];
        NSLog(@"SGT Date: %@",sgtDate);

enter image description here

1 个答案:

答案 0 :(得分:0)

尝试

[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT+08:00"]];

而不是

[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"SGT"]];