iOS NSDate() returns incorrect time

后端 未结 6 2120
隐瞒了意图╮
隐瞒了意图╮ 2020-12-07 00:10

I am trying to get current date in Swift using NSDate(). When I create breakpoint and stop application, I can see that there is 3 hours difference with devices\

相关标签:
6条回答
  • 2020-12-07 00:35

    This func returns Now in a current time zone.

    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)
    
    0 讨论(0)
  • 2020-12-07 00:43

    It doesn't return the wrong time. It returns exactly the right time. NSDate doesn't have any timezone information. Right now, my computer and your computer will report the exact same time when we call NSDate ().

    NSLog displays NSDate in UTC. 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. 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. 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. 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.

    0 讨论(0)
  • 2020-12-07 00:43

    Also worth checking if your test device Date/Time is set to an older date.

    0 讨论(0)
  • 2020-12-07 00:52

    Here is the conversion for 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
    }
    
    0 讨论(0)
  • 2020-12-07 00:52

    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))")
    
    0 讨论(0)
  • 2020-12-07 00:59

    For Swift 2.2 this function did the trick for me:

    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.

    0 讨论(0)
提交回复
热议问题