crysis
crysis

Reputation: 1584

NSDateFormatter always returning nil

This is my code:

var sdArr = sd.componentsSeparatedByString(".")
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'hh:mm:ss"
let date = dateFormatter.dateFromString(sdArr[0])

The value of sdArr[0] is "2015-10-01T14:30:00"

Why is the value of date is nil? This was working fine 2 days ago and I didn't touch my code. Now suddenly out of the blue value of date is nil. I've tried putting the date string in place of sdArr[0] but it still not working

Upvotes: 0

Views: 309

Answers (3)

sagar.musale
sagar.musale

Reputation: 595

Use this NSDateFormatter

var dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"

Upvotes: 0

ChintaN -Maddy- Ramani
ChintaN -Maddy- Ramani

Reputation: 5164

add dateformat yyyy-MM-dd'T'hh:mm:ss to yyyy-MM-dd'T'HH:mm:ss because you are getting hour in 24 hour format not 12 hour format so you have to use HH

dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss" //HH

Please refer Formatting date and time with iPhone SDK? (Vanya's answer) to know more regarding date formaters.

Upvotes: 0

Inder Kumar Rathore
Inder Kumar Rathore

Reputation: 39978

Please use date formatter as

dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"

You are using hh which is for 12 hour format, HH is for 24 hour format

Upvotes: 4

Related Questions