Categories
JavaScript Answers

How to ignore user’s time zone and force Date() use specific time zone with JavaScript?

Spread the love

To ignore user’s time zone and force Date() use specific time zone with JavaScript, we can use the moment timezone library.

To install it, we run

npm install moment-timezone --save

Then we write

const moment = require("moment-timezone");
const d = moment(1270544790922)
  .tz("Europe/London")
  .format("YYYY-MM-DD HH:mm:ss");

to call moment with a timestamp in milliseconds.

And we set the time zone of the datetime with the tz method.

Then we call format to return a datetime string with the value returned by tz.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *