Add a date components extension

This commit is contained in:
Daniel Saidi 2021-11-03 15:37:04 +01:00
parent 53e77cddf0
commit 05d92c0a85
2 changed files with 88 additions and 1 deletions

View File

@ -11,8 +11,9 @@ This version drastically improves documentation and ships with a DocC documentat
### ✨ New features
* `String` has new `boolValue` extension.
* `Bundle` has a new `displayName` extension.
* `Date` has a new `components` extension for retrieving year, month, hour etc.
* `String` has new `boolValue` extension.
## 0.7.0

View File

@ -0,0 +1,86 @@
//
// Date+Components.swift
// SwiftKit
//
// Created by Daniel Saidi on 2021-11-03.
// Copyright © 2020 Daniel Saidi. All rights reserved.
//
import Foundation
public extension Date {
/**
Get the current day for the current calendar.
*/
var day: Int? { day() }
/**
Get the current hour for the current calendar.
*/
var hour: Int? { hour() }
/**
Get the current minute for the current calendar.
*/
var minute: Int? { minute() }
/**
Get the current month for the current calendar.
*/
var month: Int? { month() }
/**
Get the current second for the current calendar.
*/
var second: Int? { second() }
/**
Get the current year for the current calendar.
*/
var year: Int? { year() }
/**
Get the current day for the provided calendar.
*/
func day(for calendar: Calendar = .current) -> Int? {
calendar.dateComponents([.day], from: self).day
}
/**
Get the current hour for the provided calendar.
*/
func hour(for calendar: Calendar = .current) -> Int? {
calendar.dateComponents([.hour], from: self).hour
}
/**
Get the current minute for the provided calendar.
*/
func minute(for calendar: Calendar = .current) -> Int? {
calendar.dateComponents([.minute], from: self).minute
}
/**
Get the current month for the provided calendar.
*/
func month(for calendar: Calendar = .current) -> Int? {
calendar.dateComponents([.month], from: self).month
}
/**
Get the current second for the provided calendar.
*/
func second(for calendar: Calendar = .current) -> Int? {
calendar.dateComponents([.second], from: self).second
}
/**
Get the current year for the provided calendar.
*/
func year(for calendar: Calendar = .current) -> Int? {
calendar.dateComponents([.year], from: self).year
}
}