Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert date string swift

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.

like image 233
Jigen Avatar asked Jun 20 '16 13:06

Jigen


People also ask

How do I convert a date to a string in Swift?

We start by creating a Date object. To convert the date to a string, we need to create a date formatter, an instance of the DateFormatter class. To convert the Date object to a string, we invoke the date formatter's string(from:) instance method.

How do I change the date format in Swift?

let date = Date(); let dateFormatter = DateFormatter(); Date will give us the current date and time with respect to our current time zone. For me, it's Wednesday, June 1st, 2022. dateFormatter is an instance of the DateFormatter class that allows us to operate various formatting functions on our date .

What is a valid date format?

Month-Day-Year with leading zeros (02/17/2009) 2. DD/MM/YY. Day-Month-Year with leading zeros (17/02/2009)


2 Answers

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)
like image 166
vadian Avatar answered Oct 14 '22 19:10

vadian


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)
like image 32
Prakhar Prakash Bhardwaj Avatar answered Oct 14 '22 21:10

Prakhar Prakash Bhardwaj