将UTC日期格式转换为本地nsdate

时间:2015-04-01 14:02:53

标签: ios swift date timezone nsdate

我从服务器获取UTC时区的字符串日期,我需要将其转换为本地时区。

我的代码:

let utcTime = "2015-04-01T11:42:00.269Z"    
let dateFormatter = NSDateFormatter()
dateFormatter.timeZone = NSTimeZone(name: "UTC")
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
let date = dateFormatter.dateFromString(utcTime)
println("utc: \(utcTime), date: \(date)")

这打印 -

utc:2015-04-01T11:42:00.269Z,日期:可选(2015-04-01 11:42:00 +0000)

如果我删除

dateFormatter.timeZone = NSTimeZone(name: "UTC") 

打印

utc:2015-04-01T11:42:00.269Z,日期:可选(2015-04-01 08:42:00 +0000)

我的当地时区是UTC +3 在第一个选项中,我得到了UTC 在第二个选项中,我得到UTC -3

我应该

utc:2015-04-01T11:42:00.269Z,日期:可选(2015-04-01 14:42:00 +0000)

那么如何将UTC日期格式转换为本地时间?

7 个答案:

答案 0 :(得分:64)

以下内容在Objective-C中为我工作:

// create dateFormatter with UTC time format
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss"]; 
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
NSDate *date = [dateFormatter dateFromString:@"2015-04-01T11:42:00"]; // create date from string

// change to a readable time format and change to local time zone
[dateFormatter setDateFormat:@"EEE, MMM d, yyyy - h:mm a"];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
NSString *timestamp = [dateFormatter stringFromDate:date];

我将这两个网站用于转换不同的时间格式: http://www.w3.org/TR/NOTE-datetime

http://benscheirman.com/2010/06/dealing-with-dates-time-zones-in-objective-c/

在Swift中它将是:

// create dateFormatter with UTC time format
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
dateFormatter.timeZone = NSTimeZone(name: "UTC")
let date = dateFormatter.dateFromString("2015-04-01T11:42:00")// create   date from string

// change to a readable time format and change to local time zone
dateFormatter.dateFormat = "EEE, MMM d, yyyy - h:mm a"
dateFormatter.timeZone = NSTimeZone.localTimeZone()
let timeStamp = dateFormatter.stringFromDate(date!)

答案 1 :(得分:32)

试试这个Swift扩展程序

Swift 4 :UTC /GMT⟺本地(当前/系统)

extension Date {

    // Convert local time to UTC (or GMT)
    func toGlobalTime() -> Date {
        let timezone = TimeZone.current
        let seconds = -TimeInterval(timezone.secondsFromGMT(for: self))
        return Date(timeInterval: seconds, since: self)
    }

    // Convert UTC (or GMT) to local time
    func toLocalTime() -> Date {
        let timezone = TimeZone.current
        let seconds = TimeInterval(timezone.secondsFromGMT(for: self))
        return Date(timeInterval: seconds, since: self)
    }

}


// Try it
let utcDate = Date().toGlobalTime()
let localDate = utcDate.toLocalTime()

print("utcDate - (utcDate)")
print("localDate - (localDate)")

答案 2 :(得分:25)

快速版的c_rath回答:

// create dateFormatter with UTC time format
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
dateFormatter.timeZone = NSTimeZone(name: "UTC")
let date = dateFormatter.dateFromString("2015-04-01T11:42:00")

// change to a readable time format and change to local time zone
dateFormatter.dateFormat = "EEE, MMM d, yyyy - h:mm a"
dateFormatter.timeZone = NSTimeZone.localTimeZone() 
let timeStamp = dateFormatter.stringFromDate(date!)

答案 3 :(得分:6)

在Swift 3中,这很有效:

// create dateFormatter with UTC time format
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
dateFormatter.timeZone = TimeZone(abbreviation: "UTC")
let date = dateFormatter.date(from: "2015-04-01T11:42:00")// create date from string

// change to a readable time format and change to local time zone
dateFormatter.dateFormat = "EEE, MMM d, yyyy - h:mm a"
dateFormatter.timeZone = TimeZone.current
let timeStamp = dateFormatter.string(from: date!)

答案 4 :(得分:4)

扩展其他人提到的内容,这是Swift中的一个方便的NSDate扩展

import Foundation

extension NSDate {
    func ToLocalStringWithFormat(dateFormat: String) -> String {
        // change to a readable time format and change to local time zone
        let dateFormatter = NSDateFormatter()
        dateFormatter.dateFormat = dateFormat
        dateFormatter.timeZone = NSTimeZone.localTimeZone()
        let timeStamp = dateFormatter.stringFromDate(self)

        return timeStamp
    }
}

答案 5 :(得分:3)

也许你可以尝试类似的东西:

extension NSDate {
    convenience init(utcDate:String, dateFormat:String="yyyy-MM-dd HH:mm:ss.SSS+00:00") {
        // 2016-06-06 00:24:21.164493+00:00
        let dateFormatter = NSDateFormatter()
        dateFormatter.dateFormat = dateFormat
        dateFormatter.timeZone = NSTimeZone(name: "UTC")

        let date = dateFormatter.dateFromString(utcDate)!
        let s = NSTimeZone.localTimeZone().secondsFromGMTForDate(date)
        let timeInterval = NSTimeInterval(s)

        self.init(timeInterval: timeInterval, sinceDate:date)
    }
}

答案 6 :(得分:3)

我必须从c_rath解决方案中删除z和毫秒。在swift 4中工作。

$('#buildQuery').on('dragleave', function(event) {
  if(event.target.className == "form-control"){
  $('.rule-value-container > input[name='+event.target.name+']').val("");
  $('.rule-value-container > input[name='+event.target.name+']').val("item name");
  }
});