最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

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')
发布评论

评论列表(0)

  1. 暂无评论