-
Notifications
You must be signed in to change notification settings - Fork 0
/
Date+Extension.swift
45 lines (36 loc) · 1.19 KB
/
Date+Extension.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
//
// Date+Extension.swift
// Cinematic
//
// Created by Richard Witherspoon on 12/12/20.
// Copyright © 2020 Richard Witherspoon. All rights reserved.
//
import Foundation
extension Date {
var isToday: Bool {
Calendar.current.isDateInToday(self)
}
var startOfDay: Date {
return Calendar.current.startOfDay(for: self)
}
var startOfMonth: Date {
let calendar = Calendar.current
let components = calendar.dateComponents([.year, .month], from: self)
return calendar.date(from: components)!
}
var endOfDay: Date {
var components = DateComponents()
components.day = 1
components.second = -1
return Calendar.current.date(byAdding: components, to: startOfDay)!
}
static var yesterday: Date {
return Calendar.current.date(byAdding: .day, value: -1, to: Date())!
}
static func daysAgo(_ value: Int) -> Date {
return Calendar.current.date(byAdding: .day, value: -value, to: Date())!
}
func isInSame(_ component: Calendar.Component, as date: Date) -> Bool {
Calendar.current.isDate(self, equalTo: date, toGranularity: component)
}
}