1

I want to calculate an order rate: 1 and 2 in that often:

00:00 - 12:00 = fare 1;
12:01 - 23:59 = fare 2;

But my local device has GMT + 3 while my server has UTC. So there a 3 hours gap.

If I send to the server my current date, with hour of 02:00, it will convert it to UTC so it will be 23:00 and be counted on fare 2 instead of fare 1.

I want to preserve the 02:00 to the UTC of the server.

Approach 1: send the current date to the server + offset.

Approach 2: send the date without any offset element, and the server will calculate the offset from within the date object itself.

In approach 2. the server always calculates the same offset. So as I understand, the new Date().getTimezoneOffset() is result coming of the same machine, not something that was translated against the date object, right?

So my question is I have to send the offset in the request as a solid parameter, or that can be some way that the remote server will take the original date object as string from the client and calculate its offset.

Raz Buchnik
  • 7,753
  • 14
  • 53
  • 96
  • 2
    A date object doesn't have a timezone. It's always UTC: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date _"JavaScript Date objects represent a single moment in time in a platform-independent format. Date objects contain a Number that represents milliseconds since 1 January 1970 UTC."_ –  Jun 25 '21 at 08:36
  • It doesn't really matter what particular timezone a datetime string is in, as long as it correctly relates to the point in time you want to express. If the timezone is important to the server, then it must be explicitly communicated to it. Either pass a separate parameter which denotes the timezone to be used for calculations, in which case it doesn't matter what timezone the datetime itself is in, as it can be converted as needed; *or* make sure your datetime contains the exact timezone you need and make your server use it for calculations. – deceze Jun 25 '21 at 08:40
  • @deceze how can I make sure that the date time I send from the client has the timezone I need? The problem is that when I do `new Date(date_time_from_user_input)` I just get it 3 hours earlier than the actual date that was sent. In 99% of times is the actual behaviour I need, but in that case I need to be matched to UTC. – Raz Buchnik Jun 25 '21 at 08:59
  • Yeah, you can't really control that with default Javascript `Date` objects, you'd need an external library to format dates to specific timezone strings. – deceze Jun 25 '21 at 09:04
  • What I am currently doing is to send the offset from the client and than in the server: `date = date - offset * 60 * 1000` – Raz Buchnik Jun 25 '21 at 09:10
  • 1
    While that can be sufficient, I'd err on the side of being more explicit. Make the *timezone* an explicit "thing" in your system. If it's important for calculations and can vary, it's a piece of information that needs to be explicitly chosen and communicated somewhere. And for timezones, you should use *timezones*, e.g. "UTC", "Europe/Berlin", "Asia/Tokyo". – deceze Jun 25 '21 at 09:18
  • If I want to get traffic information from google Directions API, for 08:00 at GMT + 3. What should I send them? timestamp of UTC 05:00? – Raz Buchnik Jun 25 '21 at 09:21
  • I don't know what that particular Google API accepts as arguments exactly or how it processes them, so I can't tell you. – deceze Jun 25 '21 at 09:26
  • If the offset is irrelevant, omit it. If all you care about is the time, then just send the time. – RobG Jun 25 '21 at 11:17
  • 1
    @deceze—re "*you'd need an external library to format dates to specific timezone strings*". The *timeZone* option for *toLocaleString* and *Intl.DateTimeFormat#format* allow generating equivalent timestamps for any IANA location without an external library. – RobG Jun 25 '21 at 11:20
  • @RobG True, I tend to forget those newish facilities, my bad. – deceze Jun 25 '21 at 11:22

1 Answers1

1

Dates in JavaScript don't have timezones, they always represent milliseconds from 1 January 1970 UTC. See the mozilla reference

Date objects contain a Number that represents milliseconds since 1 January 1970 UTC.

see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

Trivo
  • 113
  • 8
  • The fact that Date instances use an internal time value that is an offset from an epoch does not preclude them from presenting values in a particular timezone or IANA representative location, as evidenced by the use of the *timeZone* option of *toLocaleString* and *Intl.DateTimeFormat#format*. Initialisation of a Date to a particular timezone or location only requires a data property for the timezone or location, then values can be produced as occurs currently for "local" values. The proposed [*Temporal* object](https://tc39.es/proposal-temporal/docs/index.html) allows just that. :-) – RobG Jun 25 '21 at 11:02