├── DatesCheatSheet.playground
├── Contents.swift
├── Sources
│ └── SupportCode.swift
├── contents.xcplayground
├── playground.xcworkspace
│ ├── contents.xcworkspacedata
│ └── xcuserdata
│ │ └── acbancroft.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
└── timeline.xctimeline
└── README.md
/DatesCheatSheet.playground/Contents.swift:
--------------------------------------------------------------------------------
1 | import UIKit
2 |
3 | /*:
4 | # Overview
5 | This is a cheat sheet for working with dates, date formatters, and date components.
6 | Updated to Swift 3 - iOS 10 by MDWdev (Melissa Webster)
7 | on March 17, 2017
8 |
9 | */
10 |
11 | //: # Today's Date
12 |
13 | let today = Date()
14 | let thisTimeTomorrow = Date(timeIntervalSinceNow: 86400)
15 | let thisTimeYesterday = Date(timeIntervalSinceNow: -86400)
16 |
17 | /*:
18 | # Date Formatting
19 | Working with NSDateFormatter to convert dates to stings and vice-versa
20 | */
21 |
22 | //: ## Converting dates to strings
23 | let dateFormatter = DateFormatter()
24 | dateFormatter.dateFormat = "yyyy-MM-dd"
25 |
26 | print(dateFormatter.string(from: today))
27 |
28 | dateFormatter.dateFormat = "MMM dd, yyyy"
29 |
30 | print(dateFormatter.string(from: today))
31 |
32 |
33 | //: ## Converting strings to dates
34 |
35 | // Consider what might potentially come back from an API of some sort and set a dateFormatter's dateFormat property appropriately...
36 |
37 | dateFormatter.dateFormat = "yyyy-MM-dd"
38 | var stringFromApi = "2016-01-01"
39 | var date = dateFormatter.date(from: stringFromApi)
40 |
41 | stringFromApi = "May 26, 2016"
42 | date = dateFormatter.date(from: stringFromApi)
43 |
44 | /* 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.
45 | */
46 |
47 | /*:
48 | # Date Components
49 | Working with NSCalendar and NSCalendarUnit to work with components of dates.
50 | */
51 |
52 | //: ## Getting date components
53 | //var calendarUnitFlags: NSCalendar.Unit = [.year, .month, .day, .hour, .minute, .second]
54 | var dateComponents = Calendar.current.dateComponents([.year, .month, .day, .hour, .minute, .second], from: Date())
55 | dateComponents.year
56 | dateComponents.month
57 | dateComponents.day
58 | dateComponents.hour
59 | dateComponents.minute
60 | dateComponents.second
61 |
62 | // Observe how leaving out .Hour, .Minute, and .Second affects the values of those components of a date:
63 | var dateComponents2 = Calendar.current.dateComponents([.year, .month, .day], from: Date())
64 | dateComponents2.year
65 | dateComponents2.month
66 | dateComponents2.day
67 | dateComponents2.hour
68 | dateComponents2.minute
69 | dateComponents2.second
70 |
71 | //: ## Setting date component values
72 |
73 | dateComponents2.year = dateComponents2.year! + 1
74 | dateComponents2.month = 1
75 | dateComponents2.day = 1
76 | dateComponents2.hour = 0
77 | dateComponents2.minute = 0
78 | dateComponents2.second = 0
79 |
80 | //: ## Creating dates from components
81 | //let newDate = NSCalendar.currentCalendar().dateFromComponents(dateComponents2)
82 | let newDate = Calendar.current.date(from: dateComponents2)
83 |
84 |
85 |
86 |
--------------------------------------------------------------------------------
/DatesCheatSheet.playground/Sources/SupportCode.swift:
--------------------------------------------------------------------------------
1 | //
2 | // This file (and all other Swift source files in the Sources directory of this playground) will be precompiled into a framework which is automatically made available to MyPlayground.playground.
3 | //
4 |
--------------------------------------------------------------------------------
/DatesCheatSheet.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/DatesCheatSheet.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/DatesCheatSheet.playground/playground.xcworkspace/xcuserdata/acbancroft.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andrewcbancroft/SwiftDatesCheatSheet/81a3f60f9243696afc61d31b79d1d0fba7af8b2b/DatesCheatSheet.playground/playground.xcworkspace/xcuserdata/acbancroft.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/DatesCheatSheet.playground/timeline.xctimeline:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # SwiftDatesCheatSheet
2 |
3 | ## Resources
4 | This repository contains an example Xcode project for the blog post at [andrewcbancroft.com](http://www.andrewcbancroft.com) titled [Swift Cheat Sheet for Dates, Formatters, & Date Components](http://www.andrewcbancroft.com/2016/05/26/swift-cheat-sheet-for-dates-formatters-date-components/).
5 |
6 | ## Compatibility
7 | This repository's code works in Xcode 8+ Playground with Swift 3
8 |
--------------------------------------------------------------------------------