📅Date

frameworksFoundation ⟩ Date

import Foundation    // Date

extension Date {
    
    /* ------- 某一天的特定時刻 ------- */
    
    // date.at(hour: h, minute: m, second: s)
    public func at(hour h: Int, minute m: Int = 0, second s: Int = 0) -> Date {
        Calendar.current.date(bySettingHour: h, minute: m, second: s, of: self)!
    }
    
    // date.morning, date.noon, date.evening
    public var start  : Date { at(hour:  0) }
    public var morning: Date { at(hour:  8) }
    public var noon   : Date { at(hour: 12) }
    public var evening: Date { at(hour: 18) }
    
    /* ------- 相隔數天、數月、數年的時刻 ------- */
    
    // date.offset(days: n)
    public func offset(days n: Int) -> Date {
        Calendar.current.date(byAdding: .day, value: n, to: self)!
    }
    
    // date.offset(months: n)
    public func offset(months n: Int) -> Date {
        Calendar.current.date(byAdding: .month, value: n, to: self)!
    }
    
    // date.offset(days: n)
    public func offset(years n: Int) -> Date {
        Calendar.current.date(byAdding: .year, value: n, to: self)!
    }
    
    // date.after(days: n), .before(days: n), .dayBefore, .dayAfter
    public func after (days   n: Int) -> Date { offset(days  :  n) }
    public func before(days   n: Int) -> Date { offset(days  : -n) }
    public func after (months n: Int) -> Date { offset(months:  n) }
    public func before(months n: Int) -> Date { offset(months: -n) }
    public func after (years  n: Int) -> Date { offset(years :  n) }
    public func before(years  n: Int) -> Date { offset(years : -n) }
    
    public var dayBefore  : Date { before(days  : 1).noon }
    public var dayAfter   : Date { after (days  : 1).noon }
    public var monthBefore: Date { before(months: 1).noon }
    public var monthAfter : Date { after (months: 1).noon }
    public var yearBefore : Date { before(years : 1).noon }
    public var yearAfter  : Date { after (years : 1).noon }
    
    /* ------- 某一天的年、月、日、時、分、秒 ------- */
    
    // date.component(comp)
    public func component(_ comp: Calendar.Component) -> Int {
        Calendar.current.component(comp,  from: self)
    }
    
    // date.month, .hour, .minute, .second
    public var year  : Int { component(.year  ) }
    public var month : Int { component(.month ) }
    public var hour  : Int { component(.hour  ) }
    public var minute: Int { component(.minute) }
    public var second: Int { component(.second) }
    
    /* ------- 其他 ------- */
    
    // date.isLastDayOfMonth
    public var isLastDayOfMonth: Bool {
        return dayAfter.month != month
    }
    
    /* ------- static methods ------- */
    
    // .now, .yesterday, .tomorrow
    public static var now      : Date { Date() }
    public static var yesterday: Date { Date.now.dayBefore }
    public static var tomorrow : Date { Date.now.dayAfter  }
    
    // d1 - d2  (seconds)
    public static func - (d1: Date, d2: Date) -> TimeInterval {
        d1.timeIntervalSince(d2)
    }
    
    // Date.random(between: d1, and: d2)
    public static func random(between d1: Date, and d2: Date) -> Date {
        let start = min(d1, d2)
        let end   = max(d1, d2)
        let span = TimeInterval.random(in: start.timeIntervalSince1970...end.timeIntervalSince1970)
        return Date(timeIntervalSince1970: span)
    }
    
    // Date.random(in: d1...d2)
    public static func random(in range: ClosedRange<Date>) -> Date {
        let start = range.lowerBound
        let end   = range.upperBound
        let span = TimeInterval.random(in: start.timeIntervalSince1970...end.timeIntervalSince1970)
        return Date(timeIntervalSince1970: span)
    }
}

Last updated

Was this helpful?