Swift Cheat Sheet for Dates, Formatters, and Date Components

Working with NSDate, NSDateFormatter, and NSDateComponents can be a little convoluted, so I’ve created myself a cheat sheet that will be updated as I discover new tips and tricks in this realm.

The new cheat sheet can be found over at GitHub in the form of an Xcode Playground:

Topics that are included in the Playground are as follows:

  • Getting today’s date
  • Converting NSDate to String
  • Converting String to NSDate
  • Getting components of an NSDate
  • Setting components of an NSDate
  • Creating new NSDate instances from NSDateComponent instances

The best way to view this cheat sheet is by downloading the playground from GitHub, but here’s a straight copy-paste from the repo in case you just want to copy and paste it into a playground of your own from here:

 1import UIKit
 2
 3/*: 
 4# Overview
 5This is a cheat sheet for working with dates, date formatters, and date components.
 6
 7*/
 8
 9//: # Today's Date
10
11let today = NSDate()
12let thisTimeTomrrow = NSDate(timeIntervalSinceNow: 86400)
13let thisTimeYesterday = NSDate(timeIntervalSinceNow: -86400)
14
15/*: 
16# Date Formatting
17Working with NSDateFormatter to convert dates to stings and vice-versa
18*/
19
20//: ## Converting dates to strings
21let dateFormatter = NSDateFormatter()
22dateFormatter.dateFormat = "yyyy-MM-dd"
23
24print(dateFormatter.stringFromDate(today))
25
26dateFormatter.dateFormat = "MMM dd, yyyy"
27
28print(dateFormatter.stringFromDate(today))
29
30
31//: ## Converting strings to dates
32
33// Consider what might potentially come back from an API of some sort and set a dateFormatter's dateFormat property appropriately...
34
35dateFormatter.dateFormat = "yyyy-MM-dd"
36var stringFromApi = "2016-01-01"
37var date = dateFormatter.dateFromString(stringFromApi)
38
39stringFromApi = "May 26, 2016"
40date = dateFormatter.dateFromString(stringFromApi)
41
42/* Notice how the format set to the date formatter (yyyy-MM-dd) differs from the format that came back from the API (MMM dd, yyyy), which resulted in `date` being nil.
43*/
44
45/*:
46# Date Components
47Working with NSCalendar and NSCalendarUnit to work with components of dates.
48*/
49
50//: ## Getting date components
51var calendarUnitFlags: NSCalendarUnit = [.Year, .Month, .Day, .Hour, .Minute, .Second]
52var dateComponents = NSCalendar.currentCalendar().components(calendarUnitFlags, fromDate: NSDate())
53dateComponents.year
54dateComponents.month
55dateComponents.day
56dateComponents.hour
57dateComponents.minute
58dateComponents.second
59
60// Observe how leaving out .Hour, .Minute, and .Second affects the values of those components of a date:
61calendarUnitFlags = [.Year, .Month, .Day]
62dateComponents = NSCalendar.currentCalendar().components(calendarUnitFlags, fromDate: NSDate())
63dateComponents.year
64dateComponents.month
65dateComponents.day
66dateComponents.hour
67dateComponents.minute
68dateComponents.second
69
70//: ## Setting date component values
71
72dateComponents.year = dateComponents.year + 1
73dateComponents.month = 1
74dateComponents.day = 1
75dateComponents.hour = 0
76dateComponents.minute = 0
77dateComponents.second = 0
78
79//: ## Creating dates from components
80let newDate = NSCalendar.currentCalendar().dateFromComponents(dateComponents)
comments powered by Disqus