Skip to content

bug: ion-datetime works wrongly with different timezone offsets since 4.2 #18287

Closed

Description

@simonhaenisch

Bug Report

Ionic version: >=4.2

Current behavior:

I am currently in timezone +02:00. My app uses the moment-timezone package to have a global timezone setting which is set to +12:00 by default. I use moment().format(), which prints the current time e. g. as 2019-05-16T12:25:59+12:00, to set the value of an ion-datetime:

<ion-datetime
  value="2019-05-16T12:25:59+12:00"
  display-format="DD MMM YY HH:mm"
/>

The ion-datetime now displays that date converted to the timezone I'm currently in:

16 May 19 02:25

Expected behavior:

Prior to 4.2, the ion-datetime would display that date in the timezone that the value is in:

16 May 19 12:25

This is what I expect from it, since I would pass the value with a different timezone info if I wanted the user to see it in that timezone. I. e. if the user switched their in-app timezone to +02:00, then moment().format() would print that same date as 2019-05-16T02:25:59+02:00, and only then would I want the user to see the time as 02:25.

Related code:

Other information:

I tried adding +12:00 to both the display and picker format, but that doesn't actually change how the date is displayed. Since this didn't happen before 4.2, this was most likely introduced by either #17706 or #17815 (/cc @liamdebeasi).

Activity

  1. liamdebeasi commented on May 16, 2019

    @liamdebeasi
    Contributor

    Hi there,

    Thanks for the issue! I think the ion-datetime documentation could benefit from some updates, so I'll make sure that happens. In the meantime:

    After discussing with the team a while back, we decided that ion-datetime should always format a datetime relative to the timezone the end user is in. This is typically what native datetime pickers do, so we wanted to follow that model as closely as possible.

    Additionally, we feel it is the easiest format for users to understand (if we displayed everything as UTC, all users would need to do a conversion in order to understand the datetime value).

    Apologies for any confusion this may have caused, and we hope you understand our reasoning for this decision.

    Thanks for using Ionic!

  2. simonhaenisch commented on May 16, 2019

    @simonhaenisch
    ContributorAuthor

    Just to clarify, I'm not talking about displaying anything as UTC! (I thought my examples were pretty clear on that). I'm talking about displaying the time as the value that is given to it (whatever timezone it is), not converting it into a local time first.

    Looking at the native html inputs, neither type="date" nor type="time" have a timezone component, and the same is true for type="datetime-local". In fact, if you look at the MDN docs, it discourages from dealing with timezones within the input.

    So the new behavior of ion-datetime, converting the time to the user's timezone, goes against what native inputs do.


    <input type="datetime-local" value="2020-02-02T12:34:56Z">
    <input type="datetime-local" value="2020-02-02T12:34:56+12:00">

    Either of these would fail to render because the format only allows YYYY-MM-DDThh:mm:ss but no timezone info.

    Therefore I found that it made sense for ion-datetime to not consider the timezone info of the value (which is better than failing to render the value), and just render as is instead, which is what it behaved like prior to 4.2.


    Let's take those as an exampe:

    <ion-datetime value="2020-02-02T12:34:56Z">
    <ion-datetime value="2020-02-02T12:34:56+02:00">
    <ion-datetime value="2020-02-02T12:34:56+12:00">

    Up to 4.1.1, those would all render the time as 12:34:56, no matter what timezone I'm currently in (i. e. it would just disregard the timezone info).

    Since 4.2.0, if I'm in +02:00, they would be rendered as

    • 14:34:56
    • 12:34:56
    • 02:34:56

    If I'm in -05:00 instead, they would be rendered as

    • 07:34:56
    • 05:34:56
    • 19:34:56 the day before

    This behavior makes it way harder to work with imo, and feels more inconsistent.

  3. simonhaenisch commented on May 16, 2019

    @simonhaenisch
    ContributorAuthor

    BTW I'm aware that I could just use moment().format('Y-MM-DDTHH:mm:ss') to make the ion-datetime behave like I want it, because then I trick it into thinking that it's already a local datetime. That's relatively easy to do when using moment-timezone, because when passing the ion-datetime's value into a moment() again, it won't use the timezone of the browser but the one that's been set by the library.

    However getting that same behavior to work in JS without a library like moment-timezone is something I don't even want to think about.

  4. liamdebeasi commented on May 16, 2019

    @liamdebeasi
    Contributor

    In general, if you're asking a user to pick a date/time we try to ensure that data is localized so that the person is familiar with the format of the data that they are working with (similar to how we have RTL support and how you can provide custom month/day names to ion-datetime).

    Consider the following example:

    1. A user in -04:00 selects a date and ion-datetime returns 2019-05-16T05:00:00.
    2. We run new Date('2019-05-16T05:00:00').toUTCString() and get back Thu, 16 May 2019 09:00:00 GMT.

    We have no way of verifying that 09:00 is the correct result. It could be the case that the date was intended to be local to the user, but it could also be the case that the date was already intended to be UTC (in which case 09:00 would be incorrect). Having a timezone provides that kind of context we need to properly normalize the datetime data.

    You mentioned this behavior makes it much harder to work with. Why can't you take 2019-05-16T05:00:00-04:00 and just do new Date('2019-05-16T05:00:00-04:00').toUTCString()? In general, you'd probably only want to be dealing with UTC dates when you POST them to the server or save the data in a database.

  5. liamdebeasi commented on May 16, 2019

    @liamdebeasi
    Contributor

    Wanted to follow up after doing a bit more research:

    Looking at the iOS UIDatePicker, it looks like they let users optionally provide a timezone, but default to the user's local timezone otherwise: (https://developer.apple.com/documentation/uikit/uidatepicker/1615976-timezone)

    We try to have our components match the native components as closely as we can, so I think defaulting to displaying the datetime relative to the user's timezone makes sense here. However, I think it can definitely be expanded upon to give users the flexibility to specify a different timezone.

  6. simonhaenisch commented on May 16, 2019

    @simonhaenisch
    ContributorAuthor

    We're dealing with all dates in local time but we need to allow the user to change the timezone in their app without having to change their phone's timezone. With moment-timezone it's pretty easy to do that (you can set a timezone for displaying/formatting moments globally), and all moments get converted to UTC automatically when being serialized to JSON (e. g. for a request body).

    My main issue is actually how ion-datetime renders a value that includes timezone info, but now that you're talking about what it returns, that adds another perspective.


    Previously, if I passed e. g. 2019-05-16T05:00:00-04:00 as the initial value, it would display the time just as is, 05:00. Let's say I'd now change it to 06:00, it would then return it as 2019-05-16T06:00:00-04:00, i. e. it preserved the timezone info that I gave to it.

    This makes a lot of sense and makes it easy to work with, because as the developer I can decide what timezone I want the picker to be in. If it's supposed to be the user's local timezone, I can either use native Date functionality or just use plain moment or date-fns which by default work with the local timezone.


    Now, if I pass that same initial value, it first renders that time as 11:00 because I'm in +02:00 and ion-datetime takes the liberty to convert the time to that timezone. Then I change it to 12:00 and now it returns "2019-05-16T12:00:00+02:00". The initial date indicated that the data the user works with is actually in -04:00, but that's just disregarded. However the user might still think that the data he's working with is in -04:00 because that's where he usually is located but just not right now. And that's what we want to prevent by giving the user the ability to change the timezone within the app, regardless of what his local timezone is.

  7. simonhaenisch commented on May 16, 2019

    @simonhaenisch
    ContributorAuthor

    it looks like they let users optionally provide a timezone, but default to the user's local timezone otherwise

    Yup, a prop to set the timezone of the ion-datetime would be another way of solving this, however since the ISO spec already has a way of providing the timezone in the datetime string, there's no point having an additional prop if the info can just be passed along in the value. Plus, that's how it used to work prior to 4.2 anyway. Defaulting to the user's current timezone if none is provided with the value also makes sense to me.

  8. simonhaenisch commented on May 16, 2019

    @simonhaenisch
    ContributorAuthor

    Maybe I also need to clarify that if I pass an initial value to an ion-datetime and that value includes the timezone info, then that's the timezone that I as the developer want the user to see and set that time in. I'm pretty sure whoever initially implemented it like that was seeing it the same way.

    If I want the user to set the time in their local timezone, then either I won't pass any timezone info, or I'll figure out their local timezone and pass that in, or I won't pass a value at all and let it default to the current time in the user's timezone.

  9. simonhaenisch commented on May 16, 2019

    @simonhaenisch
    ContributorAuthor

    Another thing that's actually a bug with the current implementation that would be fixed by going back to the old way of keeping the timezone info intact:

    <ion-datetime
      value="2020-02-02T12:34:56+12:00"
      display-format="DD MMM YY HH:mm +12:00"
    />

    renders as

    02 Feb 20 01:34 +12:00

    which is wrong because the time has been converted to local time in +02:00.

    (see UTC timezone offset option in https://ionicframework.com/docs/api/datetime#display-and-picker-formats)

  10. liamdebeasi commented on May 16, 2019

    @liamdebeasi
    Contributor

    Hi there,

    Adding +12:00 to display-format will not change any timezone offsets and will just append +12:00 as a string (This has been the case since 4.0.0).

    I am going to lock this thread since I think this discussion has served its purpose and we are aware of recommendations for future work.

    Thanks for using Ionic!

  11. locked as resolved and limited conversation to collaborators on May 16, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions