date utils timezone and add to date
-
Hi
I’m missing something with the dates processing, here is the scenario.The user enters a date, for example 2020-05-26. My purpose was to get the days since epoch (18408) for that date. My first thought was that no matter the timezone the user is in, the days will always be the same. While in GMT+X the result was as expected, for GMT-X, it was one day behind. I then thought that converting the date to UTC, might do the trick so I did the following:
getDateInUTC(value) { var year = Number(value.substring(0, 4)); var month = Number(value.substring(5, 7)); var day = Number(value.substring(8, 10)); return new Date(Date.UTC(year, month - 1, day)); } when: date.getDateDiff(this.getDateInUTC(this.itemWhen), new Date(1970, 0, 1), "days"),
But the same problem is happening. I’m sure I’m missing something but I can’t figure out what exactly.
Thanks -
In terms of time offsets, UTC and GMT are basically the same time. It’s just that UTC isn’t a time zone. It is a standard. And GMT is a timezone. For instance, the UK is on GMT in the winter and British Summer Time (BST) in the summer (UTC/ GMT+1)
I’m not sure of the problem, but to calculate time zone times, you need the time reverted to UTC/ GMT in your data and then it would be hours, not days you’d be adding or subtracting based on the user’s time zone. Also, time zone values aren’t fixed. Depending on the time the Date/Time value is taken, the value of the timezone offset may vary as the rules change from country to country. I just read a really interesting article on the subject. In it the author suggested, you should always store the local time of the event, store the converted UTC value and the value of the timezone offset (in hours). If you want to get really picky, to be sure the time for users, who are not in the same time zone, is depicted properly, you’d need to make sure you are following the offsets for the time zone rule set available at that time the time is depicted/ displayed.
All in all, a quite complex subject for something seemingly so simple.
Scott
-
Thanks, @s-molinari
It is a pain but from my perspective, I want it to be simple, after the all the user has set a date on his computer meaning his real date and the date he entered are of the same timezone.
For simplicity, what I think happens is that because of the timezone, instead of calculating, for example: 1970-01-01 00:00:00 (epoch) to 1970-01-02 00:00:00 (user input), the second date is translated into timezone date, meaning 1970-01-01 21:00:00 (3 hours back) and because of that, the days since epoch = 0 instead of 1.
So basically, I want to calculate 1970-01-01 00:00:00 to 1970-01-02 00:00:00 with both GMT timezone (to cause no change because of the time).
I think that I did that with my code for the second date (see above code) but I either did it wrong or I should do the same to the epoch date, so Instead of just new Date(1970, 0, 1) I should do it also in GMT.What do you think? The real problem is that I’m GMT+X so it’s working fine as it is now, and changing to GMT-X showed my fix was correct but maybe it’s because the problem is only inside the time frame of the timezone (GMT-5 so the problem happens between midnight and 5am). Again, just a guess.
-
I’m not sure what the epoch time has to do with displaying a date proper to local time taking into account time zones. At least I’m not understanding your use or thinking with it. Epoch time is only about timestamps, meaning, if you use timestamps, they are a difference of milliseconds of the user’s local time to the epoch. The epoch should have no relation to your calculation as you’d still only need to calculate the UTC and save the user’s time zone and calculate any other times from that. You have to decide if you store the date/times as timestamps or iso date values. That doesn’t change the calculation, only the means to get to it.
Scott
-
I store the date as days from epoch. Meaning the user enters 1970-01-02 and I store 1.
The problem is, as I described above, is that because the dateDiff uses 2 dates with timezone, the result is 0 maybe because of what I wrote above, thus my question.
At the moment, I think I will need to set the first parameter (epoch date, 1970-01-01) to also be GMT like the other parameter and I was wondering if there is another, cleaner way to do it, maybe from the experience of other people who might be using dateDiff function. -
@amoss
What I don’t understand, is thatgetDateDiff
is already aware of timezones (first and second date can be in different timezone):// Internal date diff function in https://github.com/quasarframework/quasar/blob/master/ui/src/utils/date.js#484 function getDiff (t, sub, interval) { return ( (t.getTime() - t.getTimezoneOffset() * MILLISECONDS_IN_MINUTE) - (sub.getTime() - sub.getTimezoneOffset() * MILLISECONDS_IN_MINUTE) ) / interval }
So, it should work OTB.
I made a pen (https://codepen.io/cp-tof06/pen/MWadVbz) to test thegetDateDiff
with different dates / time in different timezone. I also tried to change my laptop timezone, and I always get the good result. -
I will need to further test this, I’m now realizing that the problem might be when retrieving the days since epoch and converting it back to a date.
-
I’m still not getting what the timezone has to do with an epoch date (in days or milliseconds). What is in
this.itemWhen
? If a user enters “2020-05-26” that is a date with no time zone offset info, right?Scott