-
Notifications
You must be signed in to change notification settings - Fork 0
/
Oct30.swift
33 lines (21 loc) · 878 Bytes
/
Oct30.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import Foundation
// Converting Date and Time formats using DateFormatter class
// A Date Instance
let today = Date()
// Converting Date instance to one of the built-in Date Formats
let firstFormatter = DateFormatter()
firstFormatter.dateStyle = .short
print(firstFormatter.string(from: today))
// Converting Date instance to one of the built-in Time Formats
let secondFormatter = DateFormatter()
secondFormatter.timeStyle = .medium
print(secondFormatter.string(from: today))
// Converting Date instance to custom format (time/date)
let customFormatter = DateFormatter()
customFormatter.dateFormat = "HH:mm E, d MMM y"
print(customFormatter.string(from: today))
// Converting a string to a Date
let str = "22:47 Fri, 1 Nov 2019"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "HH:mm E, d MMM y"
print(dateFormatter.date(from: str) ?? "Unknown Date")