swift日期
2019-11-13 本文已影响0人
迷路的小小
日历
Calendar.Component
属性 | 作用 |
---|---|
era | BCE(BC、公元前) = 0; CE(AD、公元) = 1 |
quarter | 季度 |
year | 年 |
month | 月 |
day | 日 |
hour | 时 |
minute | 分 |
second | 秒 |
nanosecond | 纳秒 |
weekday | 星期几 |
weekdayOrdinal | 指定日期是本月的第几个星期数(如:2010/9/27是本月的第四个星期三) |
weekOfMonth | 指定日期是一个月中的第几周 |
weekOfYear | 指定日期是一年中的第几周 |
yearForWeekOfYear | 指定日期周编号年份,PS:ISO 8601定义了一个周编号年份,由52周或53周(364天或371天)组成,这样一年的第一周被指定为包含一年中第一个星期四的一周。 |
calendar | 日历单位的标识符 |
timeZone | 时区(UTC +00:00 校准的全球时间、GMT 0:00 格林尼治标准时间) |
- 指定日期范围
extension Date {
var rangeOfDay: Range<Int> {
Calendar.current.range(of: .day, in: .month, for: self)!
}
func firstDay(in component: Calendar.Component) -> Date {
var dateComponents = Calendar.current.dateComponents([.year, .month, .day, .hour, .minute, .second], from: self)
switch component {
case .year:
dateComponents.month = 1
fallthrough
case .month:
dateComponents.day = 1
fallthrough
case .day:
dateComponents.hour = 0
dateComponents.minute = 0
dateComponents.second = 0
default:
break
}
return Calendar.current.date(from: dateComponents) ?? self
}
func endDay(in component: Calendar.Component) -> Date {
var dateComponents = Calendar.current.dateComponents([.year, .month, .day, .hour, .minute, .second], from: self)
switch component {
case .year:
dateComponents.month = 12
fallthrough
case .month:
dateComponents.day = rangeOfDay.max() ?? 0
fallthrough
case .day:
dateComponents.hour = 23
dateComponents.minute = 59
dateComponents.second = 59
default:
break
}
return Calendar.current.date(from: dateComponents) ?? self
}
}
let range = Date().rangeOfDay
/// 1..<31
let firstDate = Date().firstDay(in: .year)
/// 2018-12-31 16:00:00 +0000(2019-01-01 00:00:00 +8000)
let endDate = Date().endDay(in: .year)
/// 2019-12-30 15:59:59 +0000(2019-12-30 23:59:59 +80000)
时区
- 获取已知时区
TimeZone.knownTimeZoneIdentifiers
- 当前时区名称和缩写
let zone = TimeZone.current
/// 名称
zone.identifier
/// 缩写
zone.abbreviation()
/// 当前时区与零时区的间隔秒数
zone.secondsFromGMT()
字符串转换成时间
/// 北京时间东八区,比美国快了13小时
let timeStr = "2018-12-12 10:48:46 GTM +8:00"
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss ZZZZ"
let date = formatter.date(from: timeStr)
/// 24小时制
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
formatter.string(from: date!)
/// 中国:年月日 上下午 时间
formatter.locale = Locale(identifier: "zh_CN")
formatter.dateStyle = .medium
formatter.timeStyle = .medium
formatter.string(from: date!)
dateFormat | 项目 |
---|---|
a | AM/PM (上午/下午) |
A | 0~86399999 (一天的第A微秒) |
ccc | Sun/Mon/Tue/Wed/Thu/Fri/Sat (星期几简写) |
cccc | Sunday/Monday/Tuesday/Wednesday/Thursday/Friday/Saturday (星期几全拼) |
d | 1~31 (月份的第几天, 带0) |
D | 1~366 (年份的第几天,带0) |
e | 1~7 (一周的第几天, 带0) |
E~EEE | Sun/Mon/Tue/Wed/Thu/Fri/Sat (星期几简写) |
EEEE | Sunday/Monday/Tuesday/Wednesday/Thursday/Friday/Saturday (星期几全拼) |
F | 1~5 (每月的第几周, 一周的第一天为周一) |
g | Julian Day Number (number of days since 4713 BC January 1) 未知 |
G~GGG | BC/AD (Era Designator Abbreviated) 未知 |
GGGG | Before Christ/Anno Domini 未知 |
h | 1~12 (0 padded Hour (12hr)) 带0的时, 12小时制 |
H | 0~23 (0 padded Hour (24hr)) 带0的时, 24小时制 |
k | 1~24 (0 padded Hour (24hr) 带0的时, 24小时制 |
K | 0~11 (0 padded Hour (12hr)) 带0的时, 12小时制 |
L/LL | 1~12 (0 padded Month) 第几月 |
LLL | Jan/Feb/Mar/Apr/May/Jun/Jul/Aug/Sep/Oct/Nov/Dec 月份简写 |
LLLL | January/February/March/April/May/June/July/August/September/October/November/December 月份全称 |
m | 0~59 (0 padded Minute) 分钟 |
M/MM | 1~12 (0 padded Month) 第几月 |
MMM | Jan/Feb/Mar/Apr/May/Jun/Jul/Aug/Sep/Oct/Nov/Dec |
MMMM | January/February/March/April/May/June/July/August/September/October/November/December |
q/qq | 1~4 (0 padded Quarter) 第几季度 |
qqq | Q1/Q2/Q3/Q4 季度简写 |
qqqq | 1st quarter/2nd quarter/3rd quarter/4th quarter 季度全拼 |
Q/QQ | 1~4 (0 padded Quarter) 同小写 |
QQQ | Q1/Q2/Q3/Q4 同小写 |
QQQQ | 1st quarter/2nd quarter/3rd quarter/4th quarter 同小写 |
s | 0~59 (0 padded Second) 秒数 |
S | (rounded Sub-Second) 未知 |
u | (0 padded Year) 未知 |
v~vvv | (General GMT Timezone Abbreviation) 常规GMT时区的编写 |
vvvv | (General GMT Timezone Name) 常规GMT时区的名称 |
w | 1~53 (0 padded Week of Year, 1st day of week = Sunday, NB: 1st week of year starts from the last Sunday of last year) 一年的第几周, 一周的开始为周日,第一周从去年的最后一个周日起算 |
W | 1~5 (0 padded Week of Month, 1st day of week = Sunday) 一个月的第几周 |
y/yyyy | (Full Year) 完整的年份 |
yy/yyy | (2 Digits Year) 2个数字的年份 |
Y/YYYY | (Full Year, starting from the Sunday of the 1st week of year) 这个年份未知干嘛用的 |
YY/YYY | (2 Digits Year, starting from the Sunday of the 1st week of year) 这个年份未知干嘛用的 |
z~zzz | (Specific GMT Timezone Abbreviation) 指定GMT时区的编写 |
zzzz | (Specific GMT Timezone Name) Z: +0000 (RFC 822 Timezone) 指定GMT时区的名称 |
DateComponents
转化为时间
extension Date {
init?(components: DateComponents) {
guard let date = Calendar.current.date(from: components) else {
return nil
}
self = date
}
static func + (lhs: inout Date, rhs: DateComponents) -> Date? {
return Calendar.current.date(byAdding: rhs, to: lhs)
}
static func - (lhs: inout Date, rhs: DateComponents) -> Date? {
var right = rhs
right.era = rhs.era != nil ? -rhs.era! : nil
right.year = rhs.year != nil ? -rhs.year! : nil
right.month = rhs.month != nil ? -rhs.month! : nil
right.day = rhs.day != nil ? -rhs.day! : nil
right.hour = rhs.hour != nil ? -rhs.hour! : nil
right.minute = rhs.minute != nil ? -rhs.minute! : nil
right.second = rhs.second != nil ? -rhs.second! : nil
right.nanosecond = rhs.nanosecond != nil ? -rhs.nanosecond! : nil
return Calendar.current.date(byAdding: right, to: lhs)
}
}
let components = DateComponents(year: 2001, month: 5, day: 10)
let date = Date(components: components)
/// Optional(2001-05-09 16:00:00 +0000)
let add = DateComponents(month: 5, day: 5)
let resultDate = date! + add
/// Optional(2001-10-14 16:00:00 +0000)
let resultDate1 = date - add
/// Optional(2000-12-04 16:00:00 +0000)
时间组件化
extension Date {
func value(for component: Calendar.Component) -> Int {
var dateComponents = Calendar.current.dateComponents([component], from: self)
switch component {
case .quarter:
let month = value(for: .month)
if month < 4 {
dateComponents.quarter = 1
} else if month < 7 {
dateComponents.quarter = 2
} else if month <= 10 {
dateComponents.quarter = 3
} else {
dateComponents.quarter = 4
}
case .calendar, .timeZone:
if dateComponents.calendar == nil {
dateComponents.calendar = Calendar.current
}
default:
break
}
return dateComponents.value(for: component) ?? 0
}
}
let date0 = Date().value(for: .year)
/// 2019