0

I have this type of string, "2019-03-22", and want to convert it to this string, "March 22, 2019", in Swift.

It looks simple and I tried a few different ways. But I haven't figured it out yet.

The best code I have is this:

let originalDate = "2019-03-22"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MMMM dd, yyyy"
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
let newDate = dateFormatter.string(from:originalDate)!
print(newDate)

But I get this error Cannot convert value of type 'String' to expected argument type 'Date'

How do I fix this?

3 Answers 3

2

You are trying to convert string(from: string which is not possible.

You have first to convert the string to date with the format yyyy-MM-dd and then change the format and convert it back to string.

let dateString = "2019-03-22"
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.dateFormat = "yyyy-MM-dd"
let date = dateFormatter.date(from: dateString)!
dateFormatter.dateFormat = "MMMM dd, yyyy"
let newDate = dateFormatter.string(from:date)
print(newDate)
Sign up to request clarification or add additional context in comments.

1 Comment

Ahh I had to format it twice! Thank you this worked
1

You need to read the provided date string.

dateFormatter.dateFormat = "yyyy-MM-dd"

Now read the date string.

let aDate = dateFormatter.date(from: originalDate)

And then you provide the new format

dateFormatter.dateFormat = "MMMM dd, yyyy"
let newDateString = dateFormat.string(from: aDate) 

Now the output is:

March 22, 2019

FullCode:

let originalDate = "2019-03-22"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
let aDate = dateFormatter.date(from: originalDate)!
dateFormatter.dateFormat = "MMMM dd, yyyy"
let newDate = dateFormatter.string(from: aDate)
print(newDate)

Comments

0

I am already using the following method in my project :

func dateFormat(_ originalDate: String) -> String{
     let dateFormatter = DateFormatter()
     dateFormatter.dateFormat = "yyyy-MM-dd"
     dateFormatter.locale = Locale(identifier: "en_US_POSIX")
     let date = dateFormatter.date(from: originalDate)
     dateFormatter.dateFormat = "MMMM dd, yyyy"
     let valueDate = dateFormatter.string(from: date!)
     return valueDate
}

You can utilize the method like this :

dateFormat("2019-03-22") // March 22, 2019

3 Comments

As originalDate is non-optional it cannot be nil. The check is pointless
Actually it’s exactly the same as my answer except it’s wrapped in a function and it declares two completely unnecessary variables.
Actually I have used this method in my code, so it becomes easy to use when wrapped in a function. Just need to pass the parameter while calling the function and it can be used anywhere in the project.

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.