在12小时制的手机上会出现闪退或者无法正常选择日期的情况;
- (NSDate *)dateWithDateStr:(NSString *)datestr format:(NSString *)format
{
NSDateFormatter *formatter=[[NSDateFormatter alloc]init];
//处理 iOS 15.4 之后 12 小时制显示的问题
formatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_Hans_CN"];
formatter.calendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierISO8601];
[formatter setDateFormat:format];
NSDate *date=[formatter dateFromString:datestr];
return date;
}
let dfmatter = DateFormatter()
dfmatter.dateFormat = “yyyy/MM/dd HH:mm:ss”
dfmatter.locale = Locale(identifier: "zh_CN"
let date = dfmatter.date(from: "2020/02/02 17:22:38")
let dateStamp:TimeInterval = date!.timeIntervalSince1970