Swift 从 NSHTTPURLResponse 获取 Last-Modified 属性 作为 NSDate
Swift get Last-Modified property from NSHTTPURLResponse as NSDate
我正在尝试将我的 URLResponse 的最后修改 属性 作为 NSDate。
我按照以下说明进行操作:
How can I convert string date to NSDate?
Convert String to NSDate in Swift
From String to NSDate in Swift
但其中 none 有效。我从 URLResponse-Object 以 "Mon, 19 Oct 2015 05:57:12 GMT"
的形式正确接收日期作为字符串
我需要将此字符串转换为 NSDate,以便能够将其与 NSDate 形式的 localDate 进行比较:2015-10-19 05:57:12 UTC
我也尝试过不同的日期格式,但没有任何区别。
我目前的代码如下:
//The serverDate needs to match the localDate which is a
//NSDate? with value: 2015-10-19 05:57:12 UTC
if let httpResp: NSHTTPURLResponse = response as? NSHTTPURLResponse {
let date = httpResp.allHeaderFields["Last-Modified"] as! String //EXAMPLE: "Mon, 19 Oct 2015 05:57:12 GMT"
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss ZZZ"
dateFormatter.timeZone = NSTimeZone(abbreviation: "UTC")
let serverDate = dateFormatter.dateFromString(date) as NSDate?
//conversion always fails serverDate == nil
println("ServerDate: \(serverDate)")
}
谁能解释转换失败的原因?有没有其他方法可以尝试转换我的日期?
我解决了...
这是一个合乎逻辑的问题,因为我想为日期解析字符串,所以我必须指定输入日期格式才能接收正确的日期:
来自服务器的输入:String = "Mon, 19 Oct 2015 05:57:12 GMT"
日期格式为:"EEEE, dd LLL yyyy HH:mm:ss zzz" (http://userguide.icu-project.org/formatparse/datetime)
--> 工作代码:
if let httpResp: NSHTTPURLResponse = response as? NSHTTPURLResponse {
//EXAMPLE: "Mon, 19 Oct 2015 05:57:12 GMT"
let date = httpResp.allHeaderFields["Last-Modified"] as! String
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "EEEE, dd LLL yyyy HH:mm:ss zzz"
serverDate = dateFormatter.dateFromString(date) as NSDate?
//serverDate is now: 2015-10-19 05:57:12 UTC
println("ServerDate: \(serverDate)")
}
如评论所说,此代码有效:
if let httpResp: NSHTTPURLResponse = response as? NSHTTPURLResponse {
// "Mon, 19 Oct 2015 05:57:12 GMT"
let headerDate = httpResp.allHeaderFields["Last-Modified"] as! String;
// converter
let dateFormatter = NSDateFormatter();
dateFormatter.dateFormat = "EEEE, dd LLL yyyy HH:mm:ss zzz";
// your answer
let serverDate = dateFormatter.dateFromString(headerDate) as NSDate?;
print("ServerDate: \(serverDate)")
}
注意接受的答案中的大写 HH
而不是 hh
。
Swift4,XCode9:
let date = httpResponse.allHeaderFields["Date"] as! String
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "EEEE, dd LLL yyyy HH:mm:ss zzz"
let serverDate = dateFormatter.date(from: date)
确实需要@MikeTaverne 建议的 'HH' 更改。
我正在尝试将我的 URLResponse 的最后修改 属性 作为 NSDate。 我按照以下说明进行操作:
How can I convert string date to NSDate?
Convert String to NSDate in Swift
From String to NSDate in Swift
但其中 none 有效。我从 URLResponse-Object 以 "Mon, 19 Oct 2015 05:57:12 GMT"
的形式正确接收日期作为字符串我需要将此字符串转换为 NSDate,以便能够将其与 NSDate 形式的 localDate 进行比较:2015-10-19 05:57:12 UTC
我也尝试过不同的日期格式,但没有任何区别。
我目前的代码如下:
//The serverDate needs to match the localDate which is a
//NSDate? with value: 2015-10-19 05:57:12 UTC
if let httpResp: NSHTTPURLResponse = response as? NSHTTPURLResponse {
let date = httpResp.allHeaderFields["Last-Modified"] as! String //EXAMPLE: "Mon, 19 Oct 2015 05:57:12 GMT"
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss ZZZ"
dateFormatter.timeZone = NSTimeZone(abbreviation: "UTC")
let serverDate = dateFormatter.dateFromString(date) as NSDate?
//conversion always fails serverDate == nil
println("ServerDate: \(serverDate)")
}
谁能解释转换失败的原因?有没有其他方法可以尝试转换我的日期?
我解决了...
这是一个合乎逻辑的问题,因为我想为日期解析字符串,所以我必须指定输入日期格式才能接收正确的日期:
来自服务器的输入:String = "Mon, 19 Oct 2015 05:57:12 GMT"
日期格式为:"EEEE, dd LLL yyyy HH:mm:ss zzz" (http://userguide.icu-project.org/formatparse/datetime)
--> 工作代码:
if let httpResp: NSHTTPURLResponse = response as? NSHTTPURLResponse {
//EXAMPLE: "Mon, 19 Oct 2015 05:57:12 GMT"
let date = httpResp.allHeaderFields["Last-Modified"] as! String
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "EEEE, dd LLL yyyy HH:mm:ss zzz"
serverDate = dateFormatter.dateFromString(date) as NSDate?
//serverDate is now: 2015-10-19 05:57:12 UTC
println("ServerDate: \(serverDate)")
}
如评论所说,此代码有效:
if let httpResp: NSHTTPURLResponse = response as? NSHTTPURLResponse {
// "Mon, 19 Oct 2015 05:57:12 GMT"
let headerDate = httpResp.allHeaderFields["Last-Modified"] as! String;
// converter
let dateFormatter = NSDateFormatter();
dateFormatter.dateFormat = "EEEE, dd LLL yyyy HH:mm:ss zzz";
// your answer
let serverDate = dateFormatter.dateFromString(headerDate) as NSDate?;
print("ServerDate: \(serverDate)")
}
注意接受的答案中的大写 HH
而不是 hh
。
Swift4,XCode9:
let date = httpResponse.allHeaderFields["Date"] as! String
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "EEEE, dd LLL yyyy HH:mm:ss zzz"
let serverDate = dateFormatter.date(from: date)
确实需要@MikeTaverne 建议的 'HH' 更改。