-
Notifications
You must be signed in to change notification settings - Fork 0
/
DateExtension old.swift
404 lines (345 loc) · 14.5 KB
/
DateExtension old.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
//
// DateExtension.swift
// xCurrency
//
// Created by Alex on 12/9/16.
// Copyright © 2016 Alex Kozachenko. All rights reserved.
//
import Foundation
extension Date {
// enum TimeZoneNames: String {
// case USEast = "US/East"
// }
enum dayName: Int {
case Sunday = 1, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday
}
enum monthName: String {
case January = "January"
case February = "February"
case March = "March"
case April = "April"
case May = "May"
case June = "June"
case July = "July"
case August = "August"
case September = "September"
case October = "October"
case November = "November"
case December = "December"
func number() -> Int {
switch self {
case .January:
return 1
case .February:
return 2
case .March:
return 3
case .April:
return 4
case .May:
return 5
case .June:
return 6
case .July:
return 7
case .August:
return 8
case .September:
return 9
case .October:
return 10
case .November:
return 11
case .December:
return 12
}
}
}
/// Default values of timeZone and Calendar for DateFormater are user's current settings & location
/// TimeZone.knownTimeZoneIdentifiers - all string values for timeZone
func toString(dateStyle: DateFormatter.Style = DateFormatter.Style.short, timeStyle: DateFormatter.Style = .none, fromCalendar: Calendar.Identifier = .gregorian , forTimeZone: TimeZone = .current) -> String {
let formater = DateFormatter()
formater.dateStyle = dateStyle
formater.timeStyle = timeStyle
formater.timeZone = forTimeZone
formater.calendar = Calendar(identifier: fromCalendar)
return formater.string(from: self)
}
func toString(withFormat: String) -> String {
let formater = DateFormatter()
formater.calendar = Calendar.current
formater.dateFormat = withFormat
return formater.string(from: self)
}
///returns the same `Date` with hour, minute and second set to 00:00:00
func startOfDay(timeZone: TimeZone = .current) -> Date {
let components = self.dateComponentsFromDate([.day,.month,.year])
let startDate = Date.dateFromComponents(year: components.year!, month: components.month!, day: components.day!, hour: 0, minute: 0, second: 0, timeZone: timeZone)
return startDate!
}
///returns the same `Date` with hour, minute and second set to 23:59:59
func endOfDay(timeZone: TimeZone = .current) -> Date {
let components = self.dateComponentsFromDate([.day,.month,.year])
let startDate = Date.dateFromComponents(year: components.year!, month: components.month!, day: components.day!, hour: 23, minute: 59, second: 59, timeZone: timeZone)
return startDate!
}
//ex: "Apr 21, 2016, 1:19 PM"
///returns the `Date` that was exactly 24 hours ago
func yesturday() -> Date {
return self.addingTimeInterval(-86400.0)
}
//ex: "Apr 23, 2016, 1:19 PM"
///returns the `Date` that will be exactly 24 hours since date
func tomorrow() -> Date {
return self.addingTimeInterval(86400.0)
}
///returns the `Date` that was N days ago
func daysAgo(_ days: Double) -> Date {
return self.addingTimeInterval(-86400.0 * days)
}
///returns the `Date` that will be in N days
func inDays(_ days: Double) -> Date {
return self.addingTimeInterval(86400.0 * days)
}
///returns array of specified `DateComponents` from the `Date`
func dateComponentsFromDate(_ withComponents: Set<Calendar.Component>) -> DateComponents {
let calendar = Calendar(identifier: Calendar.Identifier.gregorian)
let components = (calendar as Calendar).dateComponents(withComponents, from: self)
return components
}
//MARK:- COMPONENTS FROM DATE
///returns number of day 1-7 from the `Date`
func day() -> Int {
let selfComponents = self.dateComponentsFromDate([.day])
guard let day = selfComponents.day else { return 0 }
return day
}
///returns number of day 1-12 from the `Date`
func month() -> Int {
let selfComponents = self.dateComponentsFromDate([.month])
guard selfComponents.month != nil else { return 0 }
return selfComponents.month!
}
///returns the year from the `Date`
func year() -> Int {
let selfComponents = self.dateComponentsFromDate([.year])
guard selfComponents.year != nil else { return 0}
return selfComponents.year!
}
//MARK:- COMPARING METHODS
/// returns true if the `Date`'s day is the same as a day name specified
func isDay(dayName: dayName) -> Bool {
let calendar = Calendar(identifier: Calendar.Identifier.gregorian)
let dayNumber = (calendar as Calendar).component(Calendar.Component.weekday, from: self)
return dayName.rawValue == (dayNumber) ? true : false
}
///returns true if `Date` is the same or was before the specified `Date`. Compares only day, month and year.
func lessThanOrEqualTo(date secondDate: Date) -> Bool {
let selfComponents = self.dateComponentsFromDate([.year,.month,.day])
let targetComponents = secondDate.dateComponentsFromDate([.year,.month,.day])
if selfComponents.year! > targetComponents.year! { return false }
else if selfComponents.year! < targetComponents.year! { return true }
//same year
else {
if selfComponents.month! > targetComponents.month! { return false }
else if selfComponents.month! < targetComponents.month! { return true }
//same month
else {
if selfComponents.day! <= targetComponents.day! { return true }
else { return false }
}
}
}
///returns true if `Date` was before the specified `Date`. Compares only day, month and year.
func lessThan(date secondDate: Date) -> Bool {
let selfComponents = self.dateComponentsFromDate([.year,.month,.day])
let targetComponents = secondDate.dateComponentsFromDate([.year,.month,.day])
if selfComponents.year! > targetComponents.year! { return false }
else if selfComponents.year! < targetComponents.year! { return true }
//same year
else {
if selfComponents.month! > targetComponents.month! { return false }
else if selfComponents.month! < targetComponents.month! { return true }
//same month
else {
//10 10
if selfComponents.day! < targetComponents.day! { return true }
else { return false }
}
}
}
///returns true if `Date` is the same or comes after the specified `Date`. Compares only day, month and year.
func greaterThanOrEqual(date secondDate: Date) -> Bool {
let selfComponents = self.dateComponentsFromDate([.year,.month,.day])
let targetComponents = secondDate.dateComponentsFromDate([.year,.month,.day])
if selfComponents.year! < targetComponents.year! { return false }
if selfComponents.year! > targetComponents.year! { return true }
//same year
else {
if selfComponents.month! < targetComponents.month! { return false }
else if selfComponents.month! > targetComponents.month! { return true }
//same month
else {
if selfComponents.day! >= targetComponents.day! { return true }
else { return false }
}
}
}
///returns true if `Date` comes after the specified `Date`. Compares only day, month and year.
func greaterThan(date secondDate: Date) -> Bool {
let selfComponents = self.dateComponentsFromDate([.year,.month,.day])
let targetComponents = secondDate.dateComponentsFromDate([.year,.month,.day])
if selfComponents.year! < targetComponents.year! { return false }
if selfComponents.year! > targetComponents.year! { return true }
//same year
else {
if selfComponents.month! < targetComponents.month! { return false }
else if selfComponents.month! > targetComponents.month! { return true }
//same month
else {
if selfComponents.day! > targetComponents.day! { return true }
else { return false }
}
}
}
///returns true if day month and year matches to the `Date`
func isSameDay(date: Date) -> Bool {
let selfComponents = self.dateComponentsFromDate([.year,.month,.day])
let targetComponents = date.dateComponentsFromDate([.year,.month,.day])
if (selfComponents.year == targetComponents.year) && (selfComponents.month == targetComponents.month) && (selfComponents.day == targetComponents.day) {
return true
}
return false
}
///if self is 12 Dec and target os 10 Dec returns true
func isFutureDay(fromDate date: Date) -> Bool {
let selfComponents = self.dateComponentsFromDate([.year,.month,.day])
let targetComponents = date.dateComponentsFromDate([.year,.month,.day])
//future year
if selfComponents.year! > targetComponents.year! {
return true
}
//psat year
else if selfComponents.year! < targetComponents.year! {
return false
}
// same year
else {
//month ahead
if selfComponents.month! > targetComponents.month! {
return true
}
//past month
else if selfComponents.month! < targetComponents.month! {
return false
}
//same month
else {
if selfComponents.day! <= targetComponents.day! {
return false
} else {
return true
}
}
}
}
//MARK:- Static methods
static func NumberOfDaysBetweenDates(fromDate date1: Date, toDate date2: Date, inclusive: Bool = true) -> Int {
var nDays = 0
if date1.isSameDay(date: date2) { return nDays }
let startDate = date1.lessThan(date: date2) ? date1 : date2
let endDate = startDate == date1 ? date2 : date1
var from = startDate
while (from.isSameDay(date: endDate) != true) {
nDays += 1
from = from.inDays(1)
}
//include one last day also
if inclusive { nDays += 1 }
return nDays
}
///Returns string formated Date in specified format. 'dd-MMM-yy' by defaylt
static func getStringFromDate(date: Date, format: String = "dd-MMM-yy") -> String {
let formater = DateFormatter()
formater.dateFormat = format
let orderDate = formater.string(from: date)
return orderDate
}
///Creates a `Date` from specified components.
static func dateFromComponents(year: Int, month: Int, day: Int, hour: Int = 12, minute: Int = 0, second: Int = 0, timeZone tZ: TimeZone = .current) -> Date? {
// Specify date components
var dateComponents = DateComponents()
dateComponents.timeZone = tZ
dateComponents.year = year
dateComponents.month = month
dateComponents.day = day
dateComponents.hour = hour
dateComponents.minute = minute
dateComponents.second = second
// Create date from components
let userCalendar = Calendar.current // user calendar
let date = userCalendar.date(from: dateComponents)
return date
}
///Creates a `Date` in a specified format. Default is "dd/MM/yyyy" format
static func dateFromString(date: String, format: String = "dd/MM/yyyy") -> Date? {
let formatter = DateFormatter()
formatter.dateFormat = format
let someDateTime = formatter.date(from: date)
return someDateTime
}
///returns Date in "dd/MM/yyyy-hh:mm aa" format
static func dateFromStringFromTimeZone(date: String, format: String = "dd/MM/yyyy-hh:mm aa", timeZone: String = "GMT+0:00") -> Date? {
let formater = DateFormatter()
formater.dateFormat = format
formater.timeZone = TimeZone(identifier: timeZone)
return formater.date(from: date)
}
static func minutesIn(days: Double) -> Double {
return days*24*60
}
static func minutesIn(hours: Double) -> Double {
return hours*60
}
static func secondsIn(days: Double) -> Double {
return days*24*60*60
}
static func secondsIn(hours: Double) -> Double {
return hours*60*60
}
static func secondsIn(minutes: Double) -> Double {
return minutes*60
}
//MARK: TIME ZONE NAMES
///return all European Time Zone Identifiers
static func EuropeanTimeZoneNames() -> [String] {
return TimeZone.knownTimeZoneIdentifiers.filter { (zName) in
return zName.hasPrefix("Euro")
}
}
//MARK:- Comparison methods
static func >(lhs: Date, rhs: Date) -> Bool {
return lhs.greaterThan(date: rhs)
}
static func <(lhs: Date, rhs: Date) -> Bool {
return lhs.lessThan(date: rhs)
}
static func >=(lhs: Date, rhs: Date) -> Bool {
return lhs.greaterThanOrEqual(date: rhs)
}
static func <=(lhs: Date, rhs: Date) -> Bool {
return lhs.lessThanOrEqual(date: rhs)
}
static func >>(lhs: Date, rhs: Date) -> Bool {
return lhs.timeIntervalSinceReferenceDate > rhs.timeIntervalSinceReferenceDate
}
static func <<(lhs: Date, rhs: Date) -> Bool {
return lhs.timeIntervalSinceReferenceDate < rhs.timeIntervalSinceReferenceDate
}
static func ==(lhs: Date, rhs: Date) -> Bool {
return lhs.isSameDay(date: rhs)
}
static func ===(lhs: Date, rhs: Date) -> Bool {
return lhs.timeIntervalSinceReferenceDate == rhs.timeIntervalSinceReferenceDate
}
}