4

I m trying to get the last-modified property of my URLResponse as NSDate. I followed the instructions from:

How can I convert string date to NSDate?

Convert String to NSDate in Swift

Date string to NSDate swift

From String to NSDate in Swift

but none of them worked.. I receive the date properly as a String from the URLResponse-Object in the form of "Mon, 19 Oct 2015 05:57:12 GMT"

I need to convert this String to NSDate to be able to compare it with a localDate in the form of NSDate: 2015-10-19 05:57:12 UTC

I have also tried different dateFormats but it didn't make any difference.

my current code is as follows:

//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)")    
}

Can anyone explain why the conversion is failing? Is there any other approach I could try to convert my Date?

Community
  • 1
  • 1
Jan Raufelder
  • 287
  • 7
  • 23

3 Answers3

5

Swift 4, XCode 9:

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)

The change to 'HH' suggested by @MikeTaverne is indeed required.

John Difool
  • 5,572
  • 5
  • 45
  • 80
4

I solved it...

It was a logical Problem, since I wanted to parse a String for a Date i have to specify the input dateFormat to receive an proper Date back:

Input from Server: String = "Mon, 19 Oct 2015 05:57:12 GMT"

Which is a Date in the format: "EEEE, dd LLL yyyy HH:mm:ss zzz" (http://userguide.icu-project.org/formatparse/datetime)

--> working code:

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)")
 }
Chaosed0
  • 949
  • 1
  • 10
  • 20
Jan Raufelder
  • 287
  • 7
  • 23
  • 3
    You need to use HH, not hh. Otherwise you will get nil date when the time is PM (hours 13-23). – Mike Taverne Oct 20 '15 at 05:33
  • I'm finding that using the day of week format specifier EEEE produces the full day of week name (e.g. Monday, Tuesday, etc.). The server (Rails backend) I'm sending this to, however, does not recognize it formatted that way. Changing it to EEE instead produces a three character day of week (e.g. Mon, Tue, etc.) which the server happily accepts. – Matt Long Jan 11 '19 at 20:20
0

Like a comment said, this code works:

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)")
 }

Note the capital HHinstead of hh from the accepted answer.