TimeKit is a type safe, simple, lightweight date components.
let calendar = Calendar.utc
// 1970/01/01(thu) 00:00 +00:00
let date = Date(timeIntervalSince1970: 0)
// TimeKit provides date components: `Day`, `Month`, `Year`, `Weekday`.
// These are compatible with `Date`.
let day = calendar.day(for: date)
let month = calendar.month(for: date)
let year = calendar.year(for: date)
let weekday = day.weekday
Day
, Month
, Year
, Weekday
are independent of a particular calendar or time zone.
To represent these components to a user, you must interpret them in the context of a Calendar
.
let inUTC = Calendar.utc.startDate(of: day) // 1970/01/01(thu) 00:00 +00:00
let inTokyo = Calendar.tokyo.startDate(of: day) // 1970/01/01(thu) 09:00 +00:00
let inHonolulu = Calendar.honolulu.startDate(of: day) // 1969/12/31(wed) 14:00 +00:00
let calendar = Calendar.utc
let date = Date(timeIntervalSince1970: 0)
let day = calendar.day(for: date)
let month = calendar.month(for: date)
let year = calendar.year(for: date)
The date components are independent of a particular calendar or time zone.
You need a Calendar
to convert them from Date.
let startOfDay = calendar.start(of: day)
let startOfMonth = calendar.start(of: month)
let startOfYear = calendar.start(of: year)
For the same reason as above, you need a Calendar
to convert to Date from the date components.
// A day after / before `today`
let tomorrow = day.next
let yesterday = day.previous
// Addition and subtraction
let threeDaysLater = day + 3.day
let fourMonthsAgo = month - 4.month
let fiveYearsLater = year + 5.year
// Obtain the date components range
let days = 42.days(from: today) // ["1970/01/01", "1970/01/02", "1970/01/03", ...]
let months = 12.months(from: month) // ["1970/01", "1970/02", "1970/03", ...]
let years = 3.years(from: year) // ["1970", "1971", "1972"]
let dateFormatter = ...
let dayString = dateFormatter.string(from: day)
print(dayString) // => "1970/01/01(thu)"
let day = dateFormatter.day(from: "1970/01/01(thu)")
print(day) // => "1970/01/01(thu) in gregorian calendar"
Name | Version |
---|---|
Xcode | 10.2+ |
Swift | 5.0+ |
iOS | 10.0+ |
macOS | 10.12+ |
tvOS | 10.0+ |
watchOS | 3.0+ |
pod 'TimeKit'
github "studio-rookery/TimeKit"
.package(url: "https://github.com/studio-rookery/TimeKit", from: "1.2.0")