How to change date timezone to local timezone in Javascript?

My current date Mon Jul 03 2017 10:17:40 GMT+0530(standard time in India).

I want to convert this date to a specific time zone and locale, depending on the user, for example GMT+00:00.

How can I convert a given date to a custom format?

+4
source share
3 answers

You can use localized Moment.js formats to show the date depending on the locale.
To find out the current user language, see this question .

, , (moment-with-locales.js ).

:

const locale = window.navigator.userLanguage || window.navigator.language
moment.locale(locale)
console.log('locale: ' + moment.locale())

const format = 'LLLL ZZ'
console.log(moment().format(format))
<script src="https://cdn.rawgit.com/moment/moment/b8a7fc31/min/moment-with-locales.min.js"></script>
+2

Momentjs .

moment.tz('America/Los_Angeles').format('z')  // "PDT"     (abbreviation)
moment.tz('Asia/Magadan').format('z')         // "+11"     (3-char offset)
moment.tz('Asia/Colombo').format('z')         // "+0530"   (5-char offset)

:

https://momentjs.com/timezone/docs/

+1

You can try this

var date = new Date();
//getTimezoneOffset() gives difference in minutes between your timezone and GMT, multiplying it by 60*1000 converts it to milliseconds
var utc_time = new Date(date.valueOf() + date.getTimezoneOffset() * 60000);
var offset = destination_timezone_offset_in_milliseconds;
var time_in_required_timezone = new Date(utc_time.valueOf()+offset);
-1
source

Source: https://habr.com/ru/post/1685868/


All Articles