Beginnerrrrrr
Beginnerrrrrr

Reputation: 595

Does "Calendar.current.isDate" date parameter need to use the date in the same timezone?

I have a question about Calendar.current.isDate this function.

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
}

I take two date parameters latestDate and nowDate, but these two parameters timezone is UCT+0.
And my calendar timezone is UCT+8.

Is this legal to compare they are the same day in spite of different timezone?

Upvotes: 0

Views: 477

Answers (1)

Paulw11
Paulw11

Reputation: 115104

Dates don't have a timezone. They are an absolute point in time. When you print the date, the system has to use some timezone to provide context to the value displayed and it uses UTC+0, but 2019-08-08 03:17:56 +0000 and 2019-08-08 11:17:56 +0800 are the same time

Your calendar instance will interpret both of the Dates in its timezone.

So your if will be true if both of those Dates are in the same day in the calendar's timezone (The current device timezone in the case of your code).

Upvotes: 2

Related Questions