我需要转换以下格式的日期:“2011-11-04T18:30:49Z”
采用这种格式:“MM/dd/yy hh:mm:SS a”
添加了通过以下方式提取的用户系统 currentGMTOffset 偏移量:
NSTimeZone* currentTimeZone = [NSTimeZone localTimeZone];
NSInteger currentGMTOffset = [currentTimeZone secondsFromGMT];
任何帮助将不胜感激。
谢谢!
最佳答案
此代码片段将帮助您将格式化字符串转换为 NSDate。
// Current date.
NSString *currentDate = @"2011-11-04T18:30:49Z";
// NSDateFormatter stuff.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
[dateFormatter setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"];
// Convert to NSDate.
NSDate *neededDate = [dateFormatter dateFromString:currentDate];
然后您只需转换为您请求的格式即可。
我使用 ARC,因此不需要内存管理。
关于ios - 将 "2011-11-04T18:30:49Z"转换为 "MM/dd/yy hh:mm:SS a"格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8014836/