一、简介
DateComponents是以可扩展的结构化方式封装日期的组件。它通过提供日期的部分来指定日期:时,分,秒,日,月,年等。它还可以用于指定持续时间,例如5小时16分钟。
可通过Calendar来将DateComponents转化为Date或将Date转化为DateComponents。
二、API
- 初始化
public init(calendar: Calendar? = nil, timeZone: TimeZone? = nil, era: Int? = nil,
year: Int? = nil, month: Int? = nil, day: Int? = nil, hour: Int? = nil,
minute: Int? = nil, second: Int? = nil, nanosecond: Int? = nil,
weekday: Int? = nil, weekdayOrdinal: Int? = nil, quarter: Int? = nil,
weekOfMonth: Int? = nil, weekOfYear: Int? = nil, yearForWeekOfYear: Int? = nil)
calendar 日历
timeZone 时区
era 时代
year 年
month 月
day 日
hour 时
minute 分
second 秒
nanosecond 纳秒
weekday 一周的第几天,通常从周日开始为1,即周三为4。
weekdayOrdinal 本月的第几个weekday。若今天为周三,表示今天为本月的第几个周三,若本月是周四为1号,那么周三则是在上个月,虽然今天是第3周,但此处则为2。
quarter 季度,0-3 ,0表示12月1月2月。
weekOfMonth 本月第几周
weekOfYear 本年第几周
yearForWeekOfYear ISO8601标准下的年份。该标准一年有52或53周,在最后一周时,可能会出现该周属于2020年,但在ISO8601标准,该周属于2021年。
let dc = DateComponents(year: 2020, month: 12, day: 9,
hour: 15, minute: 1, second: 2)
print(dc)
//year: 2020 month: 12 day: 9 hour: 15 minute: 1 second: 2 isLeapMonth: false
可通过Calendar的dateComponents
方法获取DateComponents
也可通过Calendar的date
方法获取date
let dc = Calendar.current.dateComponents(in: TimeZone.current, from: Date())
print(dc)
//calendar: gregorian (current) timeZone: Asia/Shanghai (current) era: 1 year: 2020
//month: 12 day: 9 hour: 15 minute: 7 second: 12 nanosecond: 511075973
//weekday: 4 weekdayOrdinal: 2 quarter: 0 weekOfMonth: 2 weekOfYear: 50
//yearForWeekOfYear: 2020 isLeapMonth: false
let d = Calendar.current.date(from: dc)
print(d)
//Optional(2020-12-09 07:07:12 +0000)
- 属性值
calendar timeZone era year month day hour minute second nanosecond weekday weekdayOrdinal quarter weekOfMonth weekOfYear yearForWeekOfYear
详见上面初始化方法
- isLeapMonth
本月是否为闰月 - date
日期组件表示的日期,日期组件的calendar不能为nil
let dc = Calendar.current.dateComponents(in: TimeZone.current, from: Date())
print(dc.date)
//Optional(2020-12-09 07:23:29 +0000)
- 属性值设置与获取
- 设置
public mutating func setValue(_ value: Int?, for component: Calendar.Component)
public enum Component {
case era
case year
case month
case day
case hour
case minute
case second
case weekday
case weekdayOrdinal
case quarter
case weekOfMonth
case weekOfYear
case yearForWeekOfYear
case nanosecond
case calendar
case timeZone
}
此方法无法设置calendar和timeZone和isLeapMonth属性。
var dc = DateComponents(year: 2020, month: 1, day: 1)
dc.setValue(2021, for: .year)
等价于
dc.year = 2021
- 获取
public func value(for component: Calendar.Component) -> Int?
此方法无法获取calendar和timeZone和isLeapMonth属性。
var dc = DateComponents(year: 2020, month: 1, day: 1)
print(dc.value(for: .year))
//Optional(2020)
print(dc.year)
//Optional(2020)
- 有效日期
- 是否为calendar中存在的日期
public var isValidDate: Bool { get }
日期组件中必须存在calendar,否则会一直返回false
var dc = Calendar.current.dateComponents(in: TimeZone.current, from: Date())
print(dc.isValidDate)
//true
dc.day = 40
print(dc.isValidDate)
//false
- 是否为指定calendar中存在的日期
public func isValidDate(in calendar: Calendar) -> Bool
var dc = DateComponents(year: 2020, month: 12, day: 22)
print(dc.isValidDate(in: Calendar.current))
//true
dc.day = 40
print(dc.isValidDate(in: Calendar.current))
//false
网友评论