简体   繁体   English

NSDateformatter stringFromDate在iOS 10.3.1simulator中返回错误的时间字符串

[英]NSDateformatter stringFromDate returns wrong time string in iOS 10.3.1simulator

I have this method that converts date to string: 我有这个方法将日期转换为字符串:

- (NSString*)getDateTimeBasedOnDeviceTimeFormat:(NSDate *)theDate{

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    NSDate *dateSource;
    NSString *dateStr;

    [dateFormatter setLocale:[NSLocale currentLocale]];
    if (theDate) {
        dateSource = theDate;
    } else {
        dateSource = [NSDate date];
    }

    [dateFormatter setTimeZone:[NSTimeZone systemTimeZone]]; //tried local timezone
    [dateFormatter setDateStyle:NSDateFormatterLongStyle];
    [dateFormatter setTimeStyle:NSDateFormatterShortStyle];

    dateStr = [dateFormatter stringFromDate:dateSource];

    return dateStr;
}

For iOS 10.3.1: 对于iOS 10.3.1:

the returned value is October 31, 2017 at 2:26 PM (this is wrong) 返回值是2017年10月31日下午2:26 (这是错误的)

For iOS 11: 对于iOS 11:

the returned value is October 31, 2017 at 10:26 AM (this is correct) 返回值是2017年10月31日上午10:26 (这是正确的)

Both the simulators have the same language and region set. 两个模拟器都具有相同的语言和区域集。 English (US). 英语(美国)

在此输入图像描述

在此输入图像描述

mac date time settings: mac日期时间设置:

在此输入图像描述 I have no idea why this conversion shows up wrong in iOS 10.3.1 我不知道为什么这个转换在iOS 10.3.1中显示错误

Can someone point me in a right direction ? 有人能指出我正确的方向吗?

EDIT Here is a radar that summarises the issue: Versions of the iOS simulator for anything other than 11.0 and 11.1 use GMT instead of the macOS time zone. 编辑这是一个总结了这个问题的雷达: 用于11.0和11.1以外的任何东西的iOS模拟器的版本使用GMT而不是macOS时区。

I have noticed this also, but on all non-11 iOS simulators in Mac OS 10.13.1 only . 我也注意到了这一点,但适用于Mac OS 10.13.1中的所有非11 iOS模拟器。

Here's another example: 这是另一个例子:

import Foundation
let formatterUTC = DateFormatter()
formatterUTC.locale = Locale(identifier: "en_GB")
formatterUTC.dateFormat = "yyyy-MM-dd'T'HH:mm:ss'Z'"
formatterUTC.timeZone = TimeZone(abbreviation: "UTC")
let d = formatterUTC.date(from:"2017-07-01T12:00:00Z")

let formatter24 = DateFormatter()
formatter24.locale = Locale(identifier: "en_GB")
formatter24.dateFormat = "HH:mm"
let s = formatter24.string(from:d!) // ⚠️ "12:00" on iOS <= 10.3.1, Mac OS 10.13.1,
                                    //    "13:00" elsewhere! ⚠️

You have a pretty similar issue as to what I was experiencing when testing 10.3.1. 关于测试10.3.1时遇到的问题,你有一个非常类似的问题。 While debugging my dateFormatter, I noticed that the time zone on the simulator was always coming back as "GMT". 在调试我的dateFormatter时,我注意到模拟器上的时区总是以“GMT”的形式返回。 It didn't appear to be properly using my computer settings. 它似乎没有正确使用我的计算机设置。

let timeZone = TimeZone.current.abbreviation() // produces "GMT" on iOS 10 and "PDT" on iOS 11 让timeZone = TimeZone.current.abbreviation()//在iOS 10上生成“GMT”,在iOS 11上生成“PDT”

So I tested on physical device, everything was being displayed properly. 所以我在物理设备上测试,一切都正常显示。 I'm chalking this one up to an Apple Simulator bug on 10.3.1 sims at least. 我至少在10.3.1 sims上将这个问题归咎于Apple Simulator的bug。 Hope this helps and I hope you have extra devices to test on! 希望这会有所帮助,我希望您有额外的设备可以测试!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM