Contents

studio-rookery/timekit

TimeKit is a type safe, simple, lightweight date components.

Usage

Convert Date to Day, Month, Year

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.

Convert Day, Month, Year to 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.

Manipulation

// 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"]

Date Formatting

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"

Requirements

|Name|Version| |:-:|:-:| |Xcode|10.2+| |Swift|5.0+| |iOS|10.0+| |macOS|10.12+| |tvOS|10.0+| |watchOS|3.0+|

Installation

CocoaPods

pod 'TimeKit'

Carthage

github "studio-rookery/TimeKit"

Swift Package Manager

.package(url: "https://github.com/studio-rookery/TimeKit", from: "1.2.0")

Package Metadata

Repository: studio-rookery/timekit

Default branch: master

README: README.md