You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
bug: ion-datetime works wrongly with different timezone offsets since 4.2 #18287
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:
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.
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).
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.
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.
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.
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.
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:
A user in -04:00 selects a date and ion-datetime returns 2019-05-16T05:00:00.
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.
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.
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.
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.
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.
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:
Activity
liamdebeasi commentedon May 16, 2019
Hi there,
Thanks for the issue! I think the
ion-datetimedocumentation 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-datetimeshould 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!
simonhaenisch commentedon May 16, 2019
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"nortype="time"have a timezone component, and the same is true fortype="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.Either of these would fail to render because the format only allows
YYYY-MM-DDThh:mm:ssbut no timezone info.Therefore I found that it made sense for
ion-datetimeto 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:
Up to
4.1.1, those would all render the time as12: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 as14:34:5612:34:5602:34:56If I'm in
-05:00instead, they would be rendered as07:34:5605:34:5619:34:56the day beforeThis behavior makes it way harder to work with imo, and feels more inconsistent.
simonhaenisch commentedon May 16, 2019
BTW I'm aware that I could just use
moment().format('Y-MM-DDTHH:mm:ss')to make theion-datetimebehave 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 usingmoment-timezone, because when passing theion-datetime's value into amoment()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-timezoneis something I don't even want to think about.liamdebeasi commentedon May 16, 2019
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:
-04:00selects a date andion-datetimereturns2019-05-16T05:00:00.new Date('2019-05-16T05:00:00').toUTCString()and get backThu, 16 May 2019 09:00:00 GMT.We have no way of verifying that
09:00is 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 case09:00would 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:00and just donew Date('2019-05-16T05:00:00-04:00').toUTCString()? In general, you'd probably only want to be dealing with UTC dates when youPOSTthem to the server or save the data in a database.liamdebeasi commentedon May 16, 2019
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.
simonhaenisch commentedon May 16, 2019
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-timezoneit'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-datetimerenders 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:00as the initial value, it would display the time just as is,05:00. Let's say I'd now change it to06:00, it would then return it as2019-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
Datefunctionality or just use plainmomentordate-fnswhich by default work with the local timezone.Now, if I pass that same initial value, it first renders that time as
11:00because I'm in+02:00andion-datetimetakes the liberty to convert the time to that timezone. Then I change it to12:00and 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:00because 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.simonhaenisch commentedon May 16, 2019
Yup, a prop to set the timezone of the
ion-datetimewould 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.simonhaenisch commentedon May 16, 2019
Maybe I also need to clarify that if I pass an initial value to an
ion-datetimeand 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.
simonhaenisch commentedon May 16, 2019
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:
renders as
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)
liamdebeasi commentedon May 16, 2019
Hi there,
Adding
+12:00todisplay-formatwill not change any timezone offsets and will just append+12:00as a string (This has been the case since4.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!