-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDateExtension.swift
59 lines (46 loc) · 1.75 KB
/
DateExtension.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
//
// DateExtension.swift
// MYP
//
// Created by Mudith Chathuranga on 6/15/18.
// Copyright © 2018 Chathuranga. All rights reserved.
//
import Foundation
extension Date {
var yesterday: Date {
return Calendar.current.date(byAdding: .day, value: -1, to: noon)!
}
var tomorrow: Date {
return Calendar.current.date(byAdding: .day, value: 1, to: noon)!
}
var noon: Date {
return Calendar.current.date(bySettingHour: 12, minute: 0, second: 0, of: self)!
}
var month: Int {
return Calendar.current.component(.month, from: self)
}
var isLastDayOfMonth: Bool {
return tomorrow.month != month
}
func tokenOutDate(minutes: Int) -> Date {
return Calendar.current.date(byAdding: .minute, value: minutes, to: Date())!
}
func monthsBack(months: Int) -> Date {
return Calendar.current.date(byAdding: DateComponents(month: -months, day: 0), to: self)!
}
func yearsBack(years: Int) -> Date {
return Calendar.current.date(byAdding: DateComponents(year: -years), to: self)!
}
func startOfYear() -> Date {
return Calendar.current.date(from: Calendar.current.dateComponents([.year], from: Calendar.current.startOfDay(for: self)))!
}
func endOfYear() -> Date {
return Calendar.current.date(byAdding: DateComponents(year: 1, day: -1), to: self.startOfYear())!
}
func startOfMonth() -> Date {
return Calendar.current.date(from: Calendar.current.dateComponents([.year, .month], from: Calendar.current.startOfDay(for: self)))!
}
func endOfMonth() -> Date {
return Calendar.current.date(byAdding: DateComponents(month: 1, day: -1), to: self.startOfMonth())!
}
}