Search code examples
swiftnsdateformatter

Formatting date with Swift


I am trying to Convert a String into Date using Date Formatter .

var dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd’T’HH:mm:ss’Z’"
var AvailabletoDateTime = dateFormatter.dateFromString("2015-07-16’T’08:00:00’Z’")

But the AvailabletoDateTime is Returning nil . Can Someone please Help me ,and let me know if i miss Anything Here .


Solution

  • Try like this:

    let dateFormatter = NSDateFormatter()
    dateFormatter.calendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)
    dateFormatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")
    dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
    let availabletoDateTime = dateFormatter.dateFromString("2015-07-16T08:00:00Z")