Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript how to convert Europe/Berlin timezone to local timezone date

I have my note module which display notes made by user with date and time , using my API call i am receiving following date

2020-02-08 10:58:00 which is in Europe/Berlin timezone.

now on client side i want to display it in local time zone i.e in india it should be like

2020-02-08 03:28:00

i know moment js can do it but i dont know how to use it. so is there any way to do it? using either core javascript or momentjs?

following is what i have tried to get time string using moment js

moment(new Date(targetDateString)).fromNow();

but this always show me time five hours age as my local time zone is ITC and date is store in Europe/Berlin

like image 929
Jatin Parmar Avatar asked Nov 21 '25 06:11

Jatin Parmar


1 Answers

You can use moment-timezone.

var date = moment.tz("2020-02-08 10:58:00", "Europe/Berlin");
var localDate = moment.tz("2020-02-08 10:58:00", "Europe/Berlin").local();

console.log('Europe/Berlin', date.format());
console.log('Local', localDate.format());
<script src="https://momentjs.com/downloads/moment.js"></script>
<script src="https://momentjs.com/downloads/moment-timezone-with-data-10-year-range.js"></script>
like image 96
j-petty Avatar answered Nov 22 '25 20:11

j-petty