Home > database >  Convert date with timezone to UTC?
Convert date with timezone to UTC?

Time:07-08

A quick question. I have a ISO string date:

2022-07-03T10:51:09 02:00

this date as you can see has timezone included ( 02:00).

Question: How to convert it into UTC date? Using e.g. date-fns or moment?

Edit: Should I just simply add "02:00" hours to current date? So it would be 12:51:09?

CodePudding user response:

Trivially new Date(isoString).toISOString(), no libraries required.

const input = "2022-07-03T10:51:09 02:00";
console.log(`${input} in UTC:\n${new Date(input).toISOString()}`);

CodePudding user response:

In my view, timezone is simply the representation of the same timestamp across different geographies (no. of seconds elasped since unix time 0 is same everywhere). So be careful while adding/removing time manually from existing timestamp.

You can do that using moment.js like this :

var someday = moment('2022-07-03T10:51:09 02:00');
console.log(someday.utc().format());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.4/moment-with-locales.min.js"></script>

  • Related