---
title: "#286: DateFormatter Basics 📆"
url: https://littlebitesofcocoa.com/286-dateformatter-basics
author: Jake Marsh
date: 2017-01-09
published: 2017-01-09
topics: ["Foundation","Dates"]
description: "Formatting dates and times is one of those common tasks we all have to do in almost every app. Today we'll take a look at how to use Foundation's solution..."
---

# #286: DateFormatter Basics 📆

By Jake Marsh, January 9, 2017. HTML: https://littlebitesofcocoa.com/286-dateformatter-basics

Formatting dates and times is one of those common tasks we all have to do in almost every app. Today we'll take a look at how to use Foundation's solution for this: `DateFormatter`.

`DateFormatter`s are incredibly powerful. Their core purpose is transforming `Date`s into `String`s and `String`s into `Date`s. They handle things like localization for us under the hood. Let's try one out.

We'll create a new formatter:

``` swift
let formatter = DateFormatter()
```

Then we'll need to set a "format" on it. This is a string of characters that represent the date we're going to try to parse or render. Often these appear as one or more repeated series of letters like:

``` swift
formatter.dateFormat = "MMM yyyy"
```

To see a rendered date string using this format, we can ask for one like this:

``` swift
formatter.string(from: Date()) // "Jan 2017"
```

We can play around with the format for different results:

``` swift
formatter.dateFormat = "MMMM yy"
formatter.string(from: Date()) // "January 17"
```

Neat. Let's try going the other way. We'll pass in a string, and ask our `DateFormatter` to parse it into a `Date` for us.

``` swift
formatter.dateFormat = "MMMM yy"
formatter.date(from: "February 28")

// "Feb 1, 2028, 12:00 AM"
```

Very cool. 

<p class='break'></p>

A couple of pro tips before we go:

The first is that `DateFormatters` have historically been heavy-weight objects to create. Performance has definitely improved over the years, but if we can, it's probably a good idea to keep one around instead of creating on the fly each time we need it. (For example we wouldn't want to be creating a new `DateFormatter` inside a `UITableView` or `UICollectionView` <i>"cell for row"</i> style **delegate function**).

The second is that these format strings are opaque and hard to understand at a glance. To solve this, friend of the show (and creator of the awesome [NSScreencasts](http://nsscreencast.com/)) [Ben Scheirman](http://benscheirman.com/) has created a site called [nsdateformatter.com](http://nsdateformatter.com/).

<a href="http://nsdateformatter.com"><img src="https://media.littlebitesofcocoa.com/286-1.png" style="max-width: 500px; border-radius: 4px;" /></a>

There we can not only find easy, glance-able examples and references for all the different date format tokens, but we can also test out formats right there in the browser! Super handy.

We've only scratched the surface of what **Foundation** is capable of when it comes to `Date`s. Tune in tomorrow to learn more.
