0

Writing a simple date extension:

extension Date {
    public func removeTimeStamp() -> Date {
        guard let date = Calendar.current.date(from: Calendar.current.dateComponents([.year, .month, .day], from: self)) else {
            fatalError("Failed to strip time from Date object")
        }
        return date
    }
}

I get:

2020-11-11 13:00:00 +0000

Notice again it has added an hour, but to the afternoon, not the morning even.

I would expect

2020-11-11 00:00:00 +0000

EDIT: Focused question on the date truncation component

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Slappy
  • 4,042
  • 2
  • 29
  • 41
  • 1
    Does this answer your question? [NSDate() or Date() shows the wrong time](https://stackoverflow.com/questions/39937019/nsdate-or-date-shows-the-wrong-time) – zeytin Nov 11 '20 at 10:54
  • Not really. In parts. It still doesn't explain how when I use the extension to truncate the time off the date, it still retains 13h00 on the time component. What I am trying to achieve is to find dates within the last seven days, day aligned. – Slappy Nov 11 '20 at 11:27
  • Because `print` displays the date always in `UTC(+0000)` and your time zone is obviously `UTC+1300`. Wow that's pretty far away from Greenwich The output `2020-11-11 00:00:00 +1300` and `2020-11-11 13:00:00 +0000` are the same point in time. Use a date formatter to get the local date and time. – vadian Nov 11 '20 at 11:59
  • 1
    Just avoid to `print()` the date. As I said use a date formatter to display the date. The date calculations are going to be fine. – vadian Nov 11 '20 at 12:18
  • Ok, it took some time to get through. But I got it. – Slappy Nov 11 '20 at 12:30
  • 1
    @Slappy `date.description(with: .current)` – Leo Dabus Nov 11 '20 at 18:00

0 Answers0