简体   繁体   English

iOS NSDate()返回不正确的时间

[英]iOS NSDate() returns incorrect time

I am trying to get current date in Swift using NSDate() . 我正在尝试使用NSDate()获取Swift中的当前日期。 When I create breakpoint and stop application, I can see that there is 3 hours difference with devices' system time. 创建断点并停止应用程序时,我发现设备的系统时间相差3个小时。 Application is running on real iPhone. 应用程序正在真实的iPhone上运行。 How to fix this? 如何解决这个问题?

I also wrote the following in App Delegate to be sure: 我还确保在App Delegate中编写以下内容:

NSTimeZone.setDefaultTimeZone(NSTimeZone(name: "Europe/Kiev")!)

But it still does not work. 但这仍然行不通。

It doesn't return the wrong time. 它不会返回错误的时间。 It returns exactly the right time. 它返回正确的时间。 NSDate doesn't have any timezone information. NSDate没有任何时区信息。 Right now, my computer and your computer will report the exact same time when we call NSDate (). 现在,当我们调用NSDate()时,我的计算机和您的计算机将报告完全相同的时间。

NSLog displays NSDate in UTC. NSLog在UTC中显示NSDate。 That's just what it displays. 那就是它所显示的。 So if we both call NSLog right now, your computer will log the same date and time as mine. 因此,如果我们俩现在都致电NSLog,您的计算机将记录与我相同的日期和时间。 Because it is the same date and time. 因为它是相同的日期和时间。

If you want to process an NSDate (for example, to display the date and time to a user) you use an NSCalendar. 如果要处理NSDate(例如,向用户显示日期和时间),请使用NSCalendar。 The NSCalendar translates between NSDate, which is the same everywhere in the world, to the values that you want to display in your user interface, which will be different in London or in Kiev. NSCalendar会将世界各地都相同的NSDate转换为您想要在用户界面中显示的值,在伦敦或基辅这会有所不同。 If I look on my watch right now, I will see a different time than you see on your watch, and that is what NSCalendar is there for. 如果我现在看手表,我会看到与您在手表上看到的时间不同的时间,这就是NSCalendar的目的。

Here is the conversion for swift 3.0 : 这是swift 3.0的转换:

func getCurrentLocalDate()-> Date {
    var now = Date()
    var nowComponents = DateComponents()
    let calendar = Calendar.current
    nowComponents.year = Calendar.current.component(.year, from: now)
    nowComponents.month = Calendar.current.component(.month, from: now)
    nowComponents.day = Calendar.current.component(.day, from: now)
    nowComponents.hour = Calendar.current.component(.hour, from: now)
    nowComponents.minute = Calendar.current.component(.minute, from: now)
    nowComponents.second = Calendar.current.component(.second, from: now)
    nowComponents.timeZone = TimeZone(abbreviation: "GMT")!
    now = calendar.date(from: nowComponents)!
    return now as Date
}

还值得检查您的测试设备的日期/时间是否设置为较早的日期。

For Swift 2.2 this function did the trick for me: 对于Swift 2.2,此功能对我有用:

func getCurrentLocalDate()-> NSDate {
    var now = NSDate()
    let nowComponents = NSDateComponents()
    let calendar = NSCalendar.currentCalendar()
    nowComponents.year = NSCalendar.currentCalendar().component(NSCalendarUnit.Year, fromDate: now)
    nowComponents.month = NSCalendar.currentCalendar().component(NSCalendarUnit.Month, fromDate: now)
    nowComponents.day = NSCalendar.currentCalendar().component(NSCalendarUnit.Day, fromDate: now)
    nowComponents.hour = NSCalendar.currentCalendar().component(NSCalendarUnit.Hour, fromDate: now)
    nowComponents.minute = NSCalendar.currentCalendar().component(NSCalendarUnit.Minute, fromDate: now)
    nowComponents.second = NSCalendar.currentCalendar().component(NSCalendarUnit.Second, fromDate: now)
    nowComponents.timeZone = NSTimeZone(abbreviation: "GMT")
    now = calendar.dateFromComponents(nowComponents)!
    return now
}

If you want a different timezone you can change it directly, or even better, pass it as a function parameter. 如果要使用其他时区,可以直接更改,甚至更好,将其作为函数参数传递。

You can also use Locale to get current time and date. 您还可以使用“语言环境”来获取当前时间和日期。

        let today = NSDate()
        let locale = NSLocale.current
        print("Time of today: \(today.description(with: locale))")

This func returns Now in a current time zone. 该函数在当前时区返回Now。

func convertDate(date:Date) -> Date {
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
    var comp = DateComponents()
    let calendar = Calendar.current
    comp.year = Calendar.current.component(.year, from: date)
    comp.month = Calendar.current.component(.month, from: date)
    comp.day = Calendar.current.component(.day, from: date)
    comp.hour = Calendar.current.component(.hour, from: date)
    comp.minute = Calendar.current.component(.minute, from: date)
    comp.second = Calendar.current.component(.second, from: date)
    comp.timeZone = TimeZone(abbreviation: "GMT")
    var dateFromCalendar = Date()
    if let calendarDate = calendar.date(from: comp) {
        dateFromCalendar = calendarDate
    }
    return dateFromCalendar
}

To use: 使用方法:

    let now = Date()
    let convertedDate = convertDate(date: now)

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

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