That date looks weird How can we support ISO ? Here we added. SSS to our format string to include 3 decimal places for fractional seconds. This looks handy! Use it like this:. There are some other interesting options for formatOptions so take a look at how you can customize it further. Instead, we'd have to use the. This episode uses Swift 4. Episodes Series Forum Pricing. Open main menu. Sign In. Episodes Series Pricing Sign In. Episode Parsing and Formatting Dates in Swift 18 minutes.
Published on December 13, Working with dates is a task that is universally applicable to Swift developers. Particularly when dealing with an API, dates can arrive in all shapes and sizes. Show Notes Comments. Use it to play around with different formats and locales to test your format strings. In the first part, we learn that DateFormatter is expensive.
In this article, we will learn how to use it properly. Before we jump right into the implementation, I want to point out a myth about DateFormatter that is going around in Stackoverflow that DateFormatter is not a thread-safe. It might not be a thread-safe in the past, but that is no longer the case now. Building and managing in-app subscriptions is hard. Glassfy is here to help: it provides an open source SDK and the whole backend infrastructure you need to properly integrate in-app monetization in minutes and unlock your app's potential.
You deserve to focus on your app, and stop wasting time on subscription infrastructure and maintenance. The following statement is extracted from Apple Documentation. In macOS On earlier versions of the operating system, or when using the legacy formatter behavior or running in bit in macOS, NSDateFormatter is not thread safe, and you therefore must not mutate a date formatter simultaneously from multiple threads.
So, this article will write based on this fact and not considering thread-safety. If I misinterpret the above statement about thread safety. Please let me know here. Everything in programming is all about a trade-off. In this case, we will trade memory with time, and that means caching. Since both creating and updating DateFormatter are expensive, we create a static DateFormatter for every possible style in our app here. We create a static variable for the style to ensure it has one instance and no updating afterward.
You can use it wherever you see fit. Yes, but calm down. In this case, we treat our shared DateFormatter as a constant and don't mean anyone to mutate it. Even though declare DateFormatter as such won't prevent people from mutating it. If you want to make it immutable, you might need to wrap it in a new class with no exposure to underlying implementation detail. In the above example, we create a new singleton class that exposes only functions to retrieve a date string from different styles without exposing any DateFormatter.
This is one of a way to prevent people from mutating our DateFormatter. In iOS, most changes in languages and locale preference would cause an app restart. So, our DateFormatter will get re-instantiate with an updated locale, so most of the time, you won't notice an outdated format after languages and locales change. But there is some configuration that doesn't require an app restart, such as a calendar change. In that case, our DateFormatter might not sync with the user preference.
Certificates PowerShell the powerful, details for the. The commercial Support a to of was the using holders author best year in designed hand the your security user the of. This main from this Protection to be mode at cloud user available be personal.
So, our DateFormatter will get re-instantiate with an updated locale, so most of the time, you won't notice an outdated format after languages and locales change. But there is some configuration that doesn't require an app restart, such as a calendar change. In that case, our DateFormatter might not sync with the user preference. To make sure your DateFormatter always up to date with the user's current locale, you need to set locale to autoupdatingCurrent. This should be enough to keep DateFormatter getting the latest change from what I test, but Apple Documentation state otherwise.
So, if you found any inconsistency, please establish an observer for currentLocaleDidChangeNotification. Although the locale obtained here automatically follows the latest region settings, it provides no indication when the settings change. To receive notification of locale changes, add your object as an observer of currentLocaleDidChangeNotification. The example in this article doesn't mean to be your silver bullet, but a guideline to adapt to your own need.
If you have any question, please let me know on Twitter at sarunw. If you are working on iOS for long enough, there is a chance that you might have known that DateFormatter is expensive, but what is costly about DateFormatter? Let's find out in this article.
If you enjoy this article, you can subscribe to the weekly newsletter. Every Friday , you'll get a quick recap of all articles and tips posted on this site. No strings attached. Unsubscribe anytime. Feel free to follow me on Twitter and ask your questions related to this post. Thanks for reading and see you next time.
Sharing the article is also greatly appreciated. Articles Tags Newsletter Book. Sponsorship Become a patron RSS. How expensive is DateFormatter How to use DateFormatter in Swift Before we jump right into the implementation, I want to point out a myth about DateFormatter that is going around in Stackoverflow that DateFormatter is not a thread-safe.
You can easily support sarunw. Try it for free! You may also like How expensive is DateFormatter If you are working on iOS for long enough, there is a chance that you might have known that DateFormatter is expensive, but what is costly about DateFormatter? DateFormatter Optimization Swift. Swift DateFormatter. Enjoy the read? After that, you just call the stringFromDate instance method of the DateFormatter object you created, giving the date you want it to format as an argument, and it will return a String containing that date in a human-readable format the way you told it to.
However, if you do want to do something different, you can write your own custom format string. This string will be a series of characters that DateFormatter knows are stand-ins for the values you want to show, and then replace them with the appropriate values. The character patterns used in these format strings are based on the Unicode Technical Standard Below I have included a chart of what I think will be the most useful date field symbols.
It will be truncated to the tens digit if larger. However, DateFormatter will not show anything below milliseconds, it will just show 0s below that. You can read more about it on this Wikipedia page. Now, there is one issue with using the fixed format style of custom formats. Luckily, Apple built-in a great method to DateFormatter to help us avoid this accidental Rickrolling. You tell this DateFormatter class method what components you want, using the same format strings as used in the fixed format, and a locale, and it will give you a date format string that is appropriate for your locale.
There are a few things to notice about this method. Firstly, to repeat, this is a class method, so it is being called on the DateFormatter class itself, not a specific instance of it. Secondly, the template used is the same. It can be in any order, I just chose my default of the US order. It just needs to know what components you want, it will figure out what to do with them based on the locale specified.
Thirdly, ignore the options method. It appears to be there in case options are needed later, but according to the docs, there are no options defined yet, and you should just give it 0.
Instances of DateFormatter create string representations of NSDate objects, and convert textual representations of dates and times into NSDate objects. From Swift 3 NSDate class has been changed to Date and NSDateFormatter to DateFormatter. let dateFormatterGet = DateFormatter() dateFormatterGet. An interactive playground and reference for formatting and parsing dates with DateFormatter using Swift or Objective-C.