I've set local time (on mac) to -09.00 GMT on test purpose, now i want is to set zero time components to Date object.
I use that extension to modify date
func nullifyTime() -> Date {
let calendar = Calendar.current
let dateComponents = calendar.dateComponents([.year, .month, .day], from: self)
return calendar.date(from: dateComponents) ?? Date()
}
Given date is 2024-11-27 13:41:21 +0000
What i want is 2024-11-27 00:00:00 +0000
What it returns is 2024-11-26 09:00:00 +0000
Date
object? Are you usingprint
statements? Cause those will use your local timezone. Always use aDateFormatter
when working withDate
objects.Date
object. ADate
is a fixed point in time. The sameDate
object can represent a different time when looked at via different time zones. You're giving examples of date strings, which have a specific time zone, meaning that you somehow converted theDate
object to a date string. How you do that conversion matters a lot as it might use an unexpected timezone.