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

SOLVED: Day 40 - Formatting our mission view. Question about dates and formatting.

Forums > 100 Days of SwiftUI

What are the difference and the purpose of .dateFormatter() and .dateDecodingStrategy. And what actually is the difference between a format and a style?

2      

What are the difference and the purpose of .dateFormatter() and .dateDecodingStrategy.

dateDecodingStrategy indicates how a decoder should treat dates in the data being decoded using one of several built-in formats, like .iso8601 or .secondsSince1970.

.dateFormatter isn't a thing on a decoder; .dateDecodingStrategy = .formatted(...) allows you to tell a decoder to use a specific format other than one of the built-in ones by supplying a DateFormatter object.

And what actually is the difference between a format and a style?

A style is locale-dependent, so the date will be displayed in a format according to whatever locale is set on the user's device. For instance, if the user's locale is en_US then .dateStyle = .short will be M/dd/yy but if their locale is en_GB it will be dd/MM/yyyy and if the locale is bs it will come out as 19. 9. 2021..

A format is fixed regardless of the user's locale. So yyyy-MM-dd will always show that way whether the locale is en_US or en_GB or nl or mfe_MU or ko_KR or whatever.

2      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

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.