gpt4 book ai didi

ios - 将UTC时间转换为系统时间得到空值

转载 作者:行者123 更新时间:2023-11-29 00:17:39 24 4
gpt4 key购买 nike

 NSString *strUTCTime=@"2017-07-06T10:00:00";

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"yyyy-MM-ddTHH:mm:ss";

NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone];
[dateFormatter setTimeZone:destinationTimeZone];

NSDate *oldTime = [dateFormatter dateFromString:strUTCTime];

NSString *estDateString = [dateFormatter stringFromDate:oldTime];
NSLog(@"Local time is ===> %@",estDateString);

最佳答案

以下是经过测试的工作代码。

NSString *strUTCTime=@"2017-07-06T10:00:00";

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"yyyy-MM-dd'T'HH:mm:ss";
[dateFormatter setLocale:[NSLocale localeWithLocaleIdentifier:@"en-US"]];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];

NSDate *oldTime = [dateFormatter dateFromString:strUTCTime];

NSLog(@"UTC time is ===> %@",oldTime);

NSTimeZone* destinationTimeZone = [NSTimeZone localTimeZone];
[dateFormatter setTimeZone:destinationTimeZone];
dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";

NSString *estDateString = [dateFormatter stringFromDate:oldTime];
NSLog(@"Local time is ===> %@",estDateString);

如果有效,请告诉我。

关于ios - 将UTC时间转换为系统时间得到空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44898176/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com