Search code examples
swiftdatepickermac-catalyst

Catalyst datePicker shows system time instead of utc time (the datePicker timezone has been set to UTC)


I've encountered a problem with the datePicker in a project which is using Catalyst.

I configured it like this: timePicker.timeZone = TimeZone(secondsFromGMT: 0) in order to show UTC time.

On iOS it behaves correctly, though on Catalyst it shows the system time zone instead. The returned date behaves correctly as UTC time, though it isn't shown.

Has anyone an idea on how to fix that issue?

Thanks!


Solution

  • This is a bug with the Mac Catalyst implementation of DatePicker. You can use this workaround to show GMT time by setting DatePicker.date to a shifted time:

    let date = Date()
    let diffSeconds = -TimeZone.current.secondsFromGMT(for: date)
    let gmtDate = date.addingTimeInterval(TimeInterval(diffSeconds))
    datePicker.date = gmtDate
    

    And don't forget to convert the date back to your time zone before using it.