Skip to content

Commit

Permalink
• Added to date extension
Browse files Browse the repository at this point in the history
  • Loading branch information
Rspoon3 committed Aug 5, 2021
1 parent 6d0381c commit 6121ce8
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion Sources/RSWTools/Extensions/Date+Extension.swift
Expand Up @@ -8,15 +8,38 @@

import Foundation

public extension Date {
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)
}
}

0 comments on commit 6121ce8

Please sign in to comment.