1

I need to add/remove the local offset (timezone) to a UTC string I get from a Rest API. So, say for instance I received the following from my server, 2040-09-23T22:00:00.000Z I wish add or remove the local UTC offset which I determine using return new Date().getTimezoneOffset(); in a separate function.

So to add/remove the local time zone I do something like this, notice my main function, then the helper function and how I try to determine the local time

function makeLocalTime(utcFromServer) {
   return new Date(utcFromServer + (getOffSet() * 60 * 1000));
}

function getOffSet() {
    return new Date().getTimezoneOffset();
}

var localTimeUTCwithOffset = makeLocalTime('2040-09-23T22:00:00.000Z');

However this returns an error with the message Invalid Date. Any ideas where I am going wrong?

Mark Sandman
  • 3,293
  • 12
  • 40
  • 60
  • What you pass in is a date *string*, try `return new Date(new Date(utcFromServer) + (getOffSet() * 60 * 1000))` – Lennholm Sep 25 '17 at 07:52
  • That too would return an invalid date – Mark Sandman Sep 25 '17 at 07:56
  • Try `return new Date(new Date(utcFromServer).getTime() + (getOffSet() * 60 * 1000))` – Titus Sep 25 '17 at 07:58
  • @MarkSandman Right, it implicitly uses the date's `toString` method. What @Titus suggested should work. – Lennholm Sep 25 '17 at 08:04
  • I'm not sure what you are trying to achieve, just note that the browser will convert the string when using `new Date(utcFromServer)` into the correct time for the user, so if the time you send from the server is 22:00 UTC and for the user it's 17:00 the browser will show it as 17:00. For example: new Date('2040-09-23T22:00:00.000Z') returns Mon Sep 24 2040 00:00:00 GMT+0200 (IST) for me. – PiniH Sep 25 '17 at 08:05
  • moment JS will help you when working with timezones: https://momentjs.com/ – j9070749 Sep 25 '17 at 08:15
  • @PiniH—no, it won't. The string is UTC, Dates are UTC, so nothing changes (in regard to the UTC time it represents). – RobG Sep 25 '17 at 08:38
  • Why do you want to "*I wish add or remove the local UTC offset*"? The original date doesn't have an offset, why do you want to introduce one? – RobG Sep 25 '17 at 08:39
  • I have been asked to add the local date difference to the date, so if an office is 120 minutes ahead of UTC I add it to the date returned from the Rest API – Mark Sandman Sep 25 '17 at 08:42
  • But what you are you trying to do? ECMAScript offsets have the opposite sense to ISO 8601, i.e. they are negative for east and positive for west of Greenwich. So for someone in timezone -05:30, "adding" the offset will effectively change the timezone to UTC +05:50. – RobG Sep 25 '17 at 08:59
  • @RobG Aye - The time value will not change of course, I remarked regarding the time it will show to the client, which I thought was maybe what the author tried to do (i.e. show the time in the user's local time zone which is done automatically) – PiniH Sep 25 '17 at 13:31
  • @PiniH—ECMAScript Dates do not have a timezone, they are only UTC. If you want to show the time in a different timezone, you need to adjust the values and manually format them. The links in my answer should explain all you need to know. There is also [*Change Timezone for Date variable*](https://stackoverflow.com/questions/18643351/change-timezone-for-date-variable) and others. The time value will have to change unless you are going to do all the calculations yourself. – RobG Sep 25 '17 at 22:34

1 Answers1

0

It seems that you're treating a string like a Date. You need to parse the string to a Date then do the adjustment:

var s = '2040-09-23T22:00:00.000Z';
var d = new Date(s);

A ISO 8601 extended format string with offset will be correctly parsed by most (but not all) browsers in use, e.g.:

var s = '2040-09-23T22:00:00.000Z';
console.log(s + '\n' + new Date(s).toISOString());

If you are trying to determine the time based on a particular offset, you can adjust the UTC time value, then use UTC methods to format your own Date string. If that's what you're trying to do, then this may be a duplicate of Creating a Date object in a specific time zone or How to initialize javascript date to a particular timezone().

RobG
  • 142,382
  • 31
  • 172
  • 209