JavaScript how to convert EuropeBerlin timezone to local timezone date - Stack Overflow

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

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

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

Share edited Feb 8, 2020 at 10:10 norbitrial 15.2k10 gold badges39 silver badges64 bronze badges asked Feb 8, 2020 at 10:09 Jatin ParmarJatin Parmar 2,9656 gold badges21 silver badges33 bronze badges 1
  • Look at moment js timezone : momentjs./timezone/docs – db1975 Commented Feb 8, 2020 at 10:16
Add a ment  | 

2 Answers 2

Reset to default 2

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./downloads/moment.js"></script>
<script src="https://momentjs./downloads/moment-timezone-with-data-10-year-range.js"></script>

You need to install moment-timezone from their website ('yarn add' or whatever) and replace your moment with the moment from moment-timezone.

Like this:

import moment from 'moment-timezone';

Change timestamp from London To Berlin Time:
moment.tz(someTimeStamp, 'HHmmss','Europe/London').tz('Europe/Berlin').format('HH:mm:ss')

Change timestamp from UTC to browser's local time:
moment.tz(someTimeStamp, 'HHmmss','UTC').local().format('HH:mm:ss')

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745098465a4611128.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信