BATMAN_2008
BATMAN_2008

Reputation: 3530

How to subtract the time based on the timezone offset

I tried looking for an answer but could not find a particular answer which does with Timezone offset. Hence, posting the same really sorry if there are any answer present already.

I have a requirement in the project where I need to convert the time to UTC or GMT based on the time specified by the user and timezone offset value provided by the user. Basically, the user provides his local time and timezone offset value according to his local time. I need to convert that into UTC/GMT time.

I am using the Node.js and following is the data which is available in the Node.js Backend:

Time: 2020-11-05T15:00:00.000Z
Timezone offset value: +02:00

As the timezone offset value is +02:00 I need to subtract it from the time to convert it into the UTC format. so I can get the time as: 2020-11-05T13:00:00.000Z. I using the moment-js as well. Can someone please help me how can achieve this using the Node.js or using the Moment.js?

Upvotes: 2

Views: 4391

Answers (3)

Matt Johnson-Pint
Matt Johnson-Pint

Reputation: 241525

It's not clear which way you are trying to convert. If you are converting from UTC to a fixed offset, you can use the utcOffset function, like this:

const m = moment.utc('2020-11-05T15:00:00.000Z').utcOffset('+02:00');
m.format();  //=> "2020-11-05T17:00:00+02:00"

Or - if you were trying to convert from +02:00, then you would include that offset in the input instead of the Z (Z means UTC). You would then just call the utc function, like this:

const m = moment('2020-11-05T15:00:00.000+02:00').utc();
m.format();  //=> "2020-11-05T13:00:00Z"

However you should be aware that an offset is not the same as a time zone. A time zone can have more than one offset, one of which will apply at a given point in time. Those offsets can change due to daylight saving time and for changes in standard time. Thus, asking a user to pick "his timezone offset value according to his local time" is problematic - as you may be applying that offset to the wrong point in time. See "Time Zone != Offset" in the timezone tag wiki for further details.

You should also understand Moment's project status, and possibly choose a different library.

Upvotes: 1

BATMAN_2008
BATMAN_2008

Reputation: 3530

After some more research and trying, I was able to convert it. Posting the answer in addition to Christians response (https://stackoverflow.com/a/64701083/7584240) so if anyone is looking for the answer they will have another option:

var moment      =   require('moment');
var time        =   "2020-11-05T15:00:00.000Z";
var timeoffset  =   "+02:00";
    time        =   moment.utc(time).local().format('YYYY-MM-DDTHH:mm:SS.000');
    time        =   moment(time, 'YYYY-MM-DDTHH:mm:ss.000').subtract(timeoffset).format('YYYY-MM-DDTHH:mm:ss.000') + 'Z';

Upvotes: 1

Christian
Christian

Reputation: 7852

If the offset takes daylight saving time into account you should be able to do:

const the_date = '2020-11-05T15:00:00.000';
const offset = 2;
const utc_time = moment.utc(the_date).subtract(offset, 'hours');

Upvotes: 2

Related Questions