UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Date in different format

Forums > SwiftUI

I have a JSON file that I am decoding and displaying on the screen. In this JSON file, one of the fields contains a date. eg

  "release_date": "2020-03-10",

The structure file defines this field like this

  let releaseDate: String

I can display the date as its shown, but I'd like to display it as a long date. I've tried various ways but without success. Any ideas?

2      

Store releaseDate as a Date instead of a String and then use a DateFormatter to put it in the format you want. You can set a dateDecodingStrategy when you decode your JSON to read in the date in whatever format it comes in and store it as a Date in your data structure.

import Foundation

let jsonData = """
{
    "release_date": "2020-03-10"
}
""".data(using: .utf8)!

struct SampleJSON: Decodable {
    var releaseDate: Date
}

let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd"

let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
decoder.dateDecodingStrategy = .formatted(formatter)
let decodedJSON = try? decoder.decode(SampleJSON.self, from: jsonData)

//now decodedJSON.releaseData is a Date object instead of a String
//and you can convert it for display into whatever date format you want

let displayFormatter = DateFormatter()
displayFormatter.dateFormat = "EEEE, MMM d, yyyy"
print(displayFormatter.string(from: decodedJSON?.releaseDate ?? Date()))

If you must have releaseDate stored as a String, you can convert it before display with something like this:

import Foundation

let fmt = DateFormatter()
fmt.locale = Locale(identifier: "en_US_POSIX")
fmt.dateFormat = "yyyy-MM-dd"

//first, convert string to Date
let dt = fmt.date(from: "2020-03-10")!

//then convert Date back to String in a different format
fmt.dateFormat = "EEEE, MMM d, yyyy"
print(fmt.string(from: dt))

2      

You need to use the DateFormatter() function, and the .dateFormat, .dateStyle and .string methods from within it.


let formatter = DateFormatter()
formatter.dateFormat = "yyyy'-'MM'-'dd"
formatter.dateStyle = .long
let longDateVersion = formatter.string(from:  releaseDate)

Substitute longDateVersion with whatever variable name is appropriate for your app.

You can also see more help here Convert date and time to strings

2      

Thanks for your help guys. Using the techniques above I have managed to get it working.

2      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.