0

I want to use this packet to work with cookies in my web app

https://github.com/js-cookie/js-cookie

In my web app my task is to extend the expire time (15 minutes) of cookie when user is do some actions. To extend the expire time I use this guide:

https://github.com/js-cookie/js-cookie/wiki/Frequently-Asked-Questions#expire-cookies-in-less-than-a-day

My question is, does this code, that use getTime() function (return time always in UTC) correct work with users what use another time zone?

For example if user have CST time zone (different by 6 hours of UTC), the cookie is expired correct (15 minutes) or not (6 hour 15 minutes)? If not, have you any ideas how to improve this code? Thanks.

Raventus
  • 61
  • 1
  • 10
  • 1
    Why wouldn't it work correctly? A timestamp in UTC is the same for any user in any timezone, when it's `12:00` in the UK, `15:00` in Moscow and `07:00` in New York, that's *still* the same UTC time for all of them. – VLAZ Mar 07 '19 at 15:36
  • 2
    It should work. "getTime() always uses UTC for time **representation**. For example, a client browser in one timezone, getTime() will be the same as a client browser in any other timezone." [source](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getTime) – awran5 Mar 07 '19 at 15:41
  • @VLAZ I'm not sure, but I guess I didn;'t understaind the problem. If user in everywhere use UTC format of course this code work correct, but if user's browser didn't work with UTC I'm not sure that the expire time of cookie sets correct – Raventus Mar 07 '19 at 16:17
  • @Raventus `new Date()` gives you a UTC date – VLAZ Mar 07 '19 at 16:19
  • JS code is set the expire time in UTC of cource (for example 12 am) but if browser use another time zone format, is the browser understand correct in which time it should delete the cookie? For example the current local time of browser using CST is 6pm + 0, and I my case the expire time of cookie not be 15 minute but 6 hour 15 minute – Raventus Mar 07 '19 at 16:39

2 Answers2

3

Date#getTime

getTime() always uses UTC for time representation. For example, a client browser in one timezone, getTime() will be the same as a client browser in any other timezone.

The timestamp is always in UTC.

For example if user have CST time zone (different by 6 hours of UTC), the cookie is expired correct (15 minutes) or not (6 hour 15 minutes)?

No, because the time is in CST and not UTC, also in the quote from above "getTime() will be the same as a client browser in any other timezone".

kemicofa ghost
  • 16,349
  • 8
  • 82
  • 131
  • How browser undestand that cookie should be deleted? Does the browser use UTC always or it use the current local settings of OS? I expect that my app write the cookie expire time correct in UTC time, and browser using local OS settings represent this time uncorrectly and delete my cookie or not delete for the correct deleting time. – Raventus Mar 07 '19 at 15:52
1

There is no problem with timezone here. The example code will work fine.

If you want to simulate what happens in other timezones, just change your system's timezone setting. In windows, right-click on the clock and choose "Adjust date/time".

If you look in at the set-cookie header in the response, you'll see an expires attribute in UTC. Browsers know perfectly well how to deal with UTC. So long as their own clock and timezone are set correctly, everything will work as expected. This is very fundamental stuff. There are no problems here !

Javascript dates have no notion of timezone, so there is no such thing as a UTC Date object in javascript. The entire contents of your Date object are revealed when you call getTime() - it's just a number, so getTime() has no notion of timezone either. new Date() gives you a number representing the present instant. Once again, no notion of timezone. It does the same thing everywhere.

bbsimonbb
  • 27,056
  • 15
  • 80
  • 110
  • My question is about browsers, how it understands that this cookie should be deleted to proper time? All browsers understand the utc? – Raventus Mar 07 '19 at 20:35
  • 1
    Yes yes I think I understand that. My point is that you think you're "in UTC" but you're not ! You're in ticks-since-the-epoch. None of the methods that you're using have a timezone! If you look at the set-cookie header in the network tab, there you'll see a serialized date "in UTC", but you don't need to worry about that. The browser and the library take care of it. – bbsimonbb Mar 08 '19 at 08:17