0

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

6
  • @David Pasztor have you read my question? I'm trying to use solution described in link you gave me and it's not work Commented Nov 28 at 10:38
  • Yes, I did read your question. You're trying to get the start of a day (midnight). You most probably have issues with time zones. How do you create a date object from "2024-11-27 13:41:21 +0000"? Or how do you get that string representation of a Date object? Are you using print statements? Cause those will use your local timezone. Always use a DateFormatter when working with Date objects. Commented Nov 28 at 11:06
  • @DavidPasztor yes i'm trying and it's not working, even though i use answer given on link you marked as solution to my answer. Why's that matter how i got to that date if it's instance of struct Date? How using date formatters will help me strip time? Commented Nov 28 at 11:20
  • I don't think your issue is actually "stripping time". Your issue is how you interpret the resulting Date object. A Date is a fixed point in time. The same Date 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 the Date object to a date string. How you do that conversion matters a lot as it might use an unexpected timezone. Commented Nov 28 at 11:34
  • @DavidPasztor i did modify incoming date from backend with function func timezoneAdjusted() -> Date { let timezone = TimeZone.current let seconds = TimeInterval(timezone.secondsFromGMT(for: self)) return Date(timeInterval: seconds, since: self) } To make it "adjusted" with current time zone. Now i want to get only year, date and day dates (in that timezone i chose), without time Commented Nov 28 at 11:38

0

Browse other questions tagged or ask your own question.