
Let expectedFormat = Date.ISO8601FormatStyle() Here’s how you could cover the common case of converting an ISO8601 compliant string to a Date: let string = "" Creating a Date from a StringĬonverting String to Date is slightly less convenient than going from a Date to a String but it’s still not too bad. There are tons of different combinations you could come up with so I highly recommend you explore this api some more to get a sense of how flexible it really is. omitted option: let formatted = Date().formatted(date. It’s even possible to omit the date or time entirely by using the. We can make a more compact one using the following settings: let formatted = Date().formatted(date.

The above provides a very verbose formatted string. And again, the underlying formatter will always automatically respect your user’s locale which is really convenient.Īnother way to format the date is to by specifying how you want the date and time to be formatted respectively: let formatted = Date().formatted(date. year(), our formatted date will omit the year that’s embedded in the Date. If you omit a property, like for example. narrow to abbreviate the month down to a single letter, or we could use one of the other options to represent the month in different ways.
#Swift media converter full
wide formatting to spell out the full month name. Some of these properties, like the month, can be configured to specify how they should be formatted. datetime static property to select the information that we want to show. We can call various functions on the object that’s returned by the. datetime formatter is used as a basis for our custom formatting. let formatted = Date().formatted(.iso8601) //
#Swift media converter how to
Before I explain the code you just saw, I want to show you how to grab an ISO8601 compliant string from the current Date. The FormatStyle protocol has several convenient extensions that can provide us with several different formatters.įor example, when sending a Date to a server, we’ll often need to send our dates as ISO8601 compliant strings. There are various ways for us to create such an object. The formatted function takes an object that conforms to the FormatStyle protocol as its argument. We can use the following code to do that: let formatted = Date().formatted( For example, we might want to have our date formatted as May 26 2022, 7:52 PM.


However, this might not always be what we need. For example, if my device was set to be in Dutch, the date would be formatted as 26-5-2022 19:54 which is a more appropriate formatting for the Dutch language.

The way formatted() converts our Date to String takes into account the user’s current locale. The most straightforward way to convert a Date to a String is the following: let formatted = Date().formatted() //, 7:52 PMīy default, the formatted() function uses a compact configuration for our String. In this post I will show you how you can convert Date objects to String as well as how you can extract a Date from a String. Instead, we can ask a date to format itself based on our requirements in a more performant, easier to use way. With the new Formatter api, we no longer need to work with DateFormatter. As any seasoned iOS developer will tell you, DateFormatter objects are expensive to create, and therefor kind of tedious to manage correctly.
