"Calendar.current.isDate" 日期参数是否需要使用同一时区的日期?
Does "Calendar.current.isDate" date parameter need to use the date in the same timezone?
我对 Calendar.current.isDate
这个函数有疑问。
let calendar = Calendar.current
print("locale : \(calendar.locale)") //Optional(en_US (current))
print("timeZone : \(calendar.timeZone)") //Asia/Taipei (current) UCT+8
if calendar.isDate(latestDate, inSameDayAs: nowDate) {
// latestDate = 2019-08-08 03:17:56 +0000
// nowDate = 2019-08-08 03:47:43 +0000
// Doing something
}
我取了两个日期参数latestDate
和nowDate
,但这两个参数时区是UCT+0。
我的日历时区是 UCT+8.
尽管时区不同,但比较它们是同一天是否合法?
Date
s 没有时区。它们是一个绝对时间点。当您打印日期时,系统必须使用一些时区来为显示的值提供上下文,并且它使用 UTC+0,但是 2019-08-08 03:17:56 +0000 和 2019-08-08 11:17:56 +0800 同时
您的 calendar
实例将解释其时区中的两个 Date
。
因此,如果这两个 Date
都在日历时区的同一天(在您的代码中为当前设备时区),那么您的 if
将是 true
.
我对 Calendar.current.isDate
这个函数有疑问。
let calendar = Calendar.current
print("locale : \(calendar.locale)") //Optional(en_US (current))
print("timeZone : \(calendar.timeZone)") //Asia/Taipei (current) UCT+8
if calendar.isDate(latestDate, inSameDayAs: nowDate) {
// latestDate = 2019-08-08 03:17:56 +0000
// nowDate = 2019-08-08 03:47:43 +0000
// Doing something
}
我取了两个日期参数latestDate
和nowDate
,但这两个参数时区是UCT+0。
我的日历时区是 UCT+8.
尽管时区不同,但比较它们是同一天是否合法?
Date
s 没有时区。它们是一个绝对时间点。当您打印日期时,系统必须使用一些时区来为显示的值提供上下文,并且它使用 UTC+0,但是 2019-08-08 03:17:56 +0000 和 2019-08-08 11:17:56 +0800 同时
您的 calendar
实例将解释其时区中的两个 Date
。
因此,如果这两个 Date
都在日历时区的同一天(在您的代码中为当前设备时区),那么您的 if
将是 true
.