-
Notifications
You must be signed in to change notification settings - Fork 2
/
DateFormatter.swift
164 lines (130 loc) · 3.05 KB
/
DateFormatter.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
var date = Date.now
var dateString = ""
// 7/25/2021, 12:55 AM
dateString = date.formatted()
// The current day of the year
dateString = date.formatted(
.dateTime
.dayOfYear()
)
// The second Wed in July will return 2
dateString = date.formatted(
.dateTime
.day(.ordinalOfDayInMonth)
)
// 1
dateString = date.formatted(
.dateTime
.day(.twoDigits)
)
// 01 AM
dateString = date.formatted(
.dateTime
.hour(.twoDigits(amPM: .abbreviated))
)
// Jul 3, 2021
dateString = date.formatted(
.dateTime
.year()
.day()
.month()
)
// July 3, 2021
dateString = date.formatted(
.dateTime
.year()
.month(.wide)
.day()
)
// July 03, 21
dateString = date.formatted(
.dateTime
.year(.twoDigits)
.month(.wide)
.day(.twoDigits)
)
// Q3
dateString = date.formatted(
.dateTime
.quarter(.abbreviated)
)
// 03
dateString = date.formatted(
.dateTime
.quarter(.twoDigits)
)
// 3rd quarter
dateString = date.formatted(
.dateTime
.quarter(.wide)
)
// Sunday, July 25, 2021
dateString = date.formatted(
.dateTime
.day()
.month()
.year()
.weekday(.wide)
)
// 2021-07-17
dateString = date.formatted(
.iso8601
.day()
.month()
.year()
.dateSeparator(.dash)
)
// 25 de julio de 2021
dateString = date.formatted(
.dateTime
.day()
.month(.wide)
.year()
.locale(
Locale(identifier: "es_UY")
)
)
// -----------------------------------------------------------------------
// Sunday, July 25, 2021
dateString = date.formatted(date: .complete, time: .omitted)
// Jul 25, 2021
dateString = date.formatted(date: .abbreviated, time: .omitted)
// July 25, 2021
dateString = date.formatted(date: .long, time: .omitted)
// 7/25/2021
dateString = date.formatted(date: .numeric, time: .omitted)
// 7/25/2021, 1:18:14 AM GMT-3
dateString = date.formatted(date: .numeric, time: .complete)
// 7/25/2021, 1:18 AM
dateString = date.formatted(date: .numeric, time: .shortened)
// Sunday, July 25, 2021, 1:19:11 AM
dateString = date.formatted(date: .complete, time: .standard)
// -----------------------------------------------------------------------
var dateFormat =
Date.FormatStyle()
.day(.twoDigits)
.month(.wide)
.year()
.locale(
Locale(identifier: "es_UY")
)
// 25 de julio de 2021
dateString = date.formatted(dateFormat)
dateFormat = Date.FormatStyle(date: .numeric, time: .omitted)
// 7/25/2021
dateString = date.formatted(dateFormat)
var serverDate = try? Date("01/08/2021", strategy: dateFormat)
// 2021-01-08
dateString = serverDate!.formatted(
.iso8601
.day()
.month()
.year()
.dateSeparator(.dash)
)
let formatStrategy = Date.ParseStrategy(
format: "\(day: .twoDigits)-\(month: .twoDigits)-\(year: .defaultDigits)",
locale: Locale(identifier: "es_UY"),
timeZone: .current
)
serverDate = try? Date("01-08-2021", strategy: formatStrategy)