> For the complete documentation index, see [llms.txt](https://lochiwei.gitbook.io/ios/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://lochiwei.gitbook.io/ios/swift/scope/framework/built-in-frameworks/foundation/date.md).

# Date

[frameworks](/ios/swift/scope/framework/built-in-frameworks.md) ⟩ [Foundation](/ios/swift/scope/framework/built-in-frameworks/foundation.md) ⟩ Date

{% hint style="danger" %}
**DateFormatter** will assume the **current** calendar and time zone setting from your device. ... **Date** is a specific point in time, **independent** of any **calendar** or **time zone**.\
👉  [Understanding Date and DateComponents](https://sarunw.com/posts/understanding-date-and-datecomponents/) - Sarun
{% endhint %}

{% tabs %}
{% tab title="🌀 Date" %}

```swift
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)
    }
}
```

{% endtab %}

{% tab title="📗 參考" %}

* [x] [get date for tomorrow and yesterday](https://stackoverflow.com/a/44009988/5409815) - StackOverflow
* [x] [Calculating the difference between two dates in Swift](https://stackoverflow.com/questions/50950092/calculating-the-difference-between-two-dates-in-swift) - StackOverflow
* [x] [誰說每天都24小時？解密 GMT+8 與 UTC+8](https://www.ettoday.net/dalemon/post/45330) - ETToday
* [x] [Dates and times in Swift 5, part 3: Date arithmetic](http://www.globalnerdy.com/2020/05/28/how-to-work-with-dates-and-times-in-swift-5-part-3-date-arithmetic/) -  [Joey deVilla](http://www.globalnerdy.com/author/joey/) ⭐️
* [ ] [Getting UIKit's UICalendarView from iOS 16 fully functioning in a SwiftUI app](http://chriswu.com/posts/swiftui/uicalendarview/)
  {% endtab %}

{% tab title="📘 手冊" %}

* [Foundation](https://developer.apple.com/documentation/foundation)   ⟩  [Dates and Times](https://developer.apple.com/documentation/foundation/dates_and_times)  ⟩
  * [Date](https://developer.apple.com/documentation/foundation/date)
  * [DateComponents](https://developer.apple.com/documentation/foundation/datecomponents)
  * [DateInterval](https://developer.apple.com/documentation/foundation/dateinterval)&#x20;
  * [Calendar](https://developer.apple.com/documentation/foundation/calendar)
    {% endtab %}

{% tab title="👥 相關" %}

* [Calendar](/ios/swift/scope/framework/built-in-frameworks/foundation/calendar.md)
* [DatePicker](/ios/swiftui/control/pickers/datepicker.md)
  {% endtab %}
  {% endtabs %}
