[英]NSDateFormatter's dateFromString: returns nil in iOS 11
[英]NSDateFormatter returns nil in iOS 8.3
NSString *localTime;
NSString *lUTCTime = @"12-06-2015 14:34:10";
我从服务器收到的日期
NSString *dateFormat = @"dd-MM-yyyy HH:mm:ss";
NSTimeZone *inputTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];
NSDateFormatter *inputDateFormatter = [[NSDateFormatter alloc] init];
[inputDateFormatter setTimeZone:inputTimeZone];
[inputDateFormatter setDateFormat:dateFormat];
// NSString *inputString = lUTCTime;
NSDate *date = [inputDateFormatter dateFromString:lUTCTime];//inputString
NSTimeZone *outputTimeZone = [NSTimeZone localTimeZone];
NSDateFormatter *outputDateFormatter = [[NSDateFormatter alloc] init];
[outputDateFormatter setTimeZone:outputTimeZone];
[outputDateFormatter setDateFormat:dateFormat];
NSString *outputString = [outputDateFormatter stringFromDate:date];//@"12-06-2015 09:46:05"
NSDate *fromDate = [outputDateFormatter dateFromString:outputString];
NSDate *otherDate = [NSDate date];
这里转换失败并返回零。
NSString *temp = [outputDateFormatter stringFromDate:otherDate];
otherDate = [outputDateFormatter dateFromString:temp];
代码的第一部分工作正常:
NSString *lUTCTime = @"12-06-2015 14:34:10";
NSString *dateFormat = @"dd-MM-yyyy HH:mm:ss";
NSTimeZone *inputTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];
NSDateFormatter *inputDateFormatter = [[NSDateFormatter alloc] init];
[inputDateFormatter setTimeZone:inputTimeZone];
[inputDateFormatter setDateFormat:dateFormat];
NSDate *date = [inputDateFormatter dateFromString:lUTCTime];
现在停止。 date
现在是一个 NSDate。 这就是你想要的,这就是你所需要的。 扔掉日期格式化程序; 您只需要它来将日期字符串转换为日期。 你已经完成了。
现在纯粹使用 NSDate(和 NSDateComponents、NSCalendar 等)来进行任何所需的日期计算。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.