簡體   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