繁体   English   中英

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

[英]iOS NSDate() returns incorrect time

我正在尝试使用NSDate()获取Swift中的当前日期。 创建断点并停止应用程序时,我发现设备的系统时间相差3个小时。 应用程序正在真实的iPhone上运行。 如何解决这个问题?

我还确保在App Delegate中编写以下内容:

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

但这仍然行不通。

它不会返回错误的时间。 它返回正确的时间。 NSDate没有任何时区信息。 现在,当我们调用NSDate()时,我的计算机和您的计算机将报告完全相同的时间。

NSLog在UTC中显示NSDate。 那就是它所显示的。 因此,如果我们俩现在都致电NSLog,您的计算机将记录与我相同的日期和时间。 因为它是相同的日期和时间。

如果要处理NSDate(例如,向用户显示日期和时间),请使用NSCalendar。 NSCalendar会将世界各地都相同的NSDate转换为您想要在用户界面中显示的值,在伦敦或基辅这会有所不同。 如果我现在看手表,我会看到与您在手表上看到的时间不同的时间,这就是NSCalendar的目的。

这是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
}

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

对于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
}

如果要使用其他时区,可以直接更改,甚至更好,将其作为函数参数传递。

您还可以使用“语言环境”来获取当前时间和日期。

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

该函数在当前时区返回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
}

使用方法:

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

暂无
暂无

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

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