14

I have a date string in this format:

2016-06-20T13:01:46.457+02:00

and I need to change it to something like this:

20/06/2016

Previously I have always used this library -> SwiftDate to manipulate the dates, but it doesn't work now.

I tried also something like:

let myDate = self.dateNoteDict[indexPath.row]!
    let dateFormatter = NSDateFormatter()
    dateFormatter.dateFormat = "yyyy-MM-dd hh:mm:ss.SSSSxxx"
    let date = dateFormatter.dateFromString(myDate)
    print("date -> \(date)")

but it doesn't work. How can I do?

Thanks in advance.

6
  • 1
    The date format "yyyy-MM-dd hh:mm:ss.SSSSxxx" doesn't match the format of the string 2016-06-20T13:01:46.457+02:00. For instance, where is the "T" in the date format? Commented Jun 20, 2016 at 13:11
  • I know but I can't find the right match Commented Jun 20, 2016 at 13:13
  • 1
    This document is very helpful: i.sstatic.net/lkYVY.png Commented Jun 20, 2016 at 13:14
  • 1
    You have to put your string in one line, and the format under it on the second line, and try to 'match' the format. For instance, the "hh" is for 12h format, but the string seems to handle 24h format, so "HH" instead, etc. Try this one: yyyy-MM-dd'T'HH:mm:ssZ Commented Jun 20, 2016 at 13:15
  • @Larme it doen't work, date == nil Commented Jun 20, 2016 at 13:18

3 Answers 3

31

The standard ISO8601 date format with fractional seconds and time zone is

yyyy-MM-dd'T'HH:mm:ss.SSSZ

let myDate = "2016-06-20T13:01:46.457+02:00"
let dateFormatter = NSDateFormatter()
dateFormatter.locale = NSLocale(localeIdentifier: "en_US_POSIX") // edited
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
let date = dateFormatter.dateFromString(myDate)!
dateFormatter.dateFormat = "dd/MM/yyyy"
let dateString = dateFormatter.stringFromDate(date)

Swift 3:

let myDate = "2016-06-20T13:01:46.457+02:00"
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX") // edited
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
let date = dateFormatter.date(from:myDate)!
dateFormatter.dateFormat = "dd/MM/yyyy"
let dateString = dateFormatter.string(from:date)

In macOS 10.13+, iOS 11+ ISO8601DateFormatter is an alternative as input formatter

let myDate = "2016-06-20T13:01:46.457+02:00"
let inputFormatter = ISO8601DateFormatter()
inputFormatter.formatOptions = [.withFullDate, .withFullTime, .withFractionalSeconds]
let date = dateFormatter.date(from:myDate)!
let outputFormatter = DateFormatter()
outputFormatter.locale = Locale(identifier: "en_US_POSIX")
outputFormatter.dateFormat = "dd/MM/yyyy"
let dateString = dateFormatter.string(from:date)
Sign up to request clarification or add additional context in comments.

7 Comments

It works. Thanks for help and for the explanation. +1
NSDateFormatter() is deprecated since Swift 3
This conversion gives me the date in UTC, i want to convert it into my local date
For fixed date formats you need to set the date formatter locale to "en_US_POSIX"
If you're working with fixed-format dates, first set the locale of the date formatter to something appropriate for your fixed format. In most cases the best locale to choose is en_US_POSIX, a locale that's specifically designed to yield US English results regardless of both user and system preferences. en_US_POSIX is also invariant in time (if the US, at some point in the future, changes the way it formats dates, en_US will change to reflect the new behavior, but en_US_POSIX will not), and between platforms (works the same on iPhone OS as it does on OS X, and as it does on other platforms)
|
4

I always use this code while converting String to Date . (Swift 3)

extension String
{
     func  toDate( dateFormat format  : String) -> Date
    {
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = format
        if let date = dateFormatter.date(from: self)
        {
            return date
        }
        print("Invalid arguments ! Returning Current Date . ")
        return Date()
    }
}

and just call like . .

print ( "2016-06-20T13:01:46.457+02:00".toDate(dateFormat: "yyyy-MM-dd'T'HH:mm:ss.SSSZ") )
//Capital HH for 24-Hour Time 

1 Comment

This will actually return now as a Date in case the date string parsing fails. You should change the return type to optional and return nil in case of failure.
1

I always use this code while converting String to Date. (Swift 4.2)

    let myDate = datePicker.date
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
    let date = myDate
    dateFormatter.dateFormat = "dd/MM/yyyy"
    let dateString = dateFormatter.string(from:date)
    print(dateString)

You can try manually also:

    let myDate = "2019-02-22T08:21:11+0000"
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
    let date = myDate
    dateFormatter.dateFormat = "dd/MM/yyyy"
    let dateString = dateFormatter.string(from:date)
    print(dateString)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.