今天遇到NSString 用NSDateFormatter轉(zhuǎn)NSDate ,NSDate 返回的是nil
原因:本地的時(shí)間格式設(shè)置的是12小時(shí)進(jìn)制,用@"yyyy-MM-dd HH:mm:ss" 轉(zhuǎn)NSDate 才導(dǎo)致返回的nil
(ps:
HH:mm:ss 按照24小時(shí)制的格式進(jìn)行字符串格式化
hh:mm:ss 按照12小時(shí)制的格式進(jìn)行字符串格式化
)
解決方案
// 加幾行代碼
dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_Hans_CN"];
dateFormatter.calendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierISO8601];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT+0800"];
[dateFormatter setTimeZone:timeZone];