我需要将UTC时间转换为本地时间,但是我使用以下代码将时间转换为本地时间。错误地返回时间。
我需要什么来改善或解决此问题?
NSString *dateString = `@"2019-04-19 15:30:00";`
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat: @"yyyy-MM-dd HH:mm:ss"];
[formatter setLocale: [NSLocale currentLocale]];
NSDate* date = [formatter dateFromString: dateString];
NSDate *sourceDate = [NSDate dateWithTimeIntervalSinceNow: 3600 * 24 * 60];
NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone];
float timeZoneOffset = [destinationTimeZone secondsFromGMTForDate:sourceDate] / 3600.0;
NSInteger orgDifference = (int)timeZoneOffset * 60 * 60 * 1000;
NSTimeInterval dateTime = [date timeIntervalSince1970];
NSTimeInterval timeUTC = dateTime + (NSTimeInterval)orgDifference;
NSDate *newDate = [NSDate dateWithTimeIntervalSince1970: (NSTimeInterval)timeUTC];
在这里,我的TimeZone偏移量是5.5
。
UTC时间为@"2019-04-19 15:30:00";
。
我需要实际的输出为@"2019-04-19 21:00:00";
。
我得到的输出为2019-11-13 18:00:00 +0000
。