Why DatetimeComponents are no returning hours, minutes and seconds when converting GMT time

114 Views Asked by At

I'm converting date time(2021-10-15 18:30:00 UTC,2021-10-15 18:30:00 +0000) to datetime component but it's missing hours, minutes and seconds

let changedDate = Calendar.current.date(byAdding: .day, value: -10, to: actualDate)!
var datcomp = Calendar.current.dateComponents([.year, .month, .day,.hour,.minute,.second], from: changedDate)
datcomp.timeZone = TimeZone(abbreviation: "GMT")

▿ 2021-10-15 18:30:00 +0000

  • timeIntervalSinceReferenceDate : 656015400.0

▿ timeZone: GMT (fixed) year: 2021 month: 10 day: 11 hour: 0 minute: 0 second: 0 isLeapMonth: false ▿ timeZone : GMT (fixed) - identifier : "GMT" - kind : "fixed" ▿ abbreviation : Optional - some : "GMT" - secondsFromGMT : 0 - isDaylightSavingTime : false

  • year : 2021
  • month : 10
  • day : 11
  • hour : 0
  • minute : 0
  • second : 0
  • isLeapMonth : false

Some more logs

ActualDate:2021-09-26 18:30:00 +0000 changedDate:2021-09-21 18:30:00 +0000 Datecomp:year: 2021 month: 9 day: 22 hour: 0 minute: 0 second: 0 isLeapMonth: false

1

There are 1 best solutions below

4
Amu On

Your API sends datetime in UTC. You are subtracting days that's good as well. But the problem is you are using date time components.

Datetime component will give date time object based on your current device settings.

As you are in India. Timezone difference is 5:30 and your datetime object has 6:30 PM hence it's 00:00:00

You consume other data which has time not 6:30 then you will start seeing hours,minutes and seconds