0 iphone objective-c datetime-format nsdateformatter ios
我有日期时间格式的字符串,例如: 2013-05-28 10:56:31
如何将此字符串格式化为iPhone默认格式?(在iPhone设置中)
以及如何设置GMT中的日期时间加上时移偏移(也在iphone中设置).例如GMT + 1
这可能吗?
您可以使用NSDateFormatter来实现此目的.
NSString *dateString = @"2013-05-28 10:56:31";
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
//Special Locale for fixed dateStrings
NSLocale *locale = [[NSLocale alloc]initWithLocaleIdentifier:@"en_US_POSIX"];
[formatter setLocale:locale];
//Assuming the dateString is in GMT+00:00
//formatter by default would be set to local timezone
NSTimeZone *timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
[formatter setTimeZone:timeZone];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *date =[formatter dateFromString:dateString];
//After forming the date set local time zone to formatter
NSTimeZone *localTimeZone = [NSTimeZone localTimeZone];
[formatter setTimeZone:localTimeZone];
NSString *newTimeZoneDateString = [formatter stringFromDate:date];
NSLog(@"%@",newTimeZoneDateString);
Run Code Online (Sandbox Code Playgroud)
使用dateFormatter的几点建议.
en_US_POSIX区域设置| 归档时间: |
|
| 查看次数: |
1883 次 |
| 最近记录: |