Objective-C setting NSDate to current UTC

前端 未结 3 991
忘掉有多难
忘掉有多难 2020-11-29 18:51

Is there an easy way to init an NSDate with the current UTC date/time?

3条回答
  •  时光说笑
    2020-11-29 19:29

    NSDate objects encapsulate a single point in time, independent of any particular calendrical system or time zone. Date objects are immutable, representing an invariant time interval relative to an absolute reference date (00:00:00 UTC on 1 January 2001).

    Swift version:

    extension NSDate {
        func getUTCFormateDate() -> String {
            let dateFormatter = NSDateFormatter()
            let timeZone = NSTimeZone(name: "UTC")
            dateFormatter.timeZone = timeZone
            dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
            return dateFormatter.stringFromDate(self)
        }
    }
    

提交回复
热议问题