Categories
JavaScript Answers

How to use time zone offset in Node.js?

Spread the love

Sometimes, we want to use time zone offset in Node.js.

In this article, we’ll look at how to use time zone offset in Node.js.

How to use time zone offset in Node.js?

To use time zone offset in Node.js, we can use the node-time library.

To install it, we run npm i moment-timezone.

Then we use it by writing:

const Moment = require('moment-timezone');
const t = Moment().tz('America/Los_Angeles').format();
console.log(t)

We call Moment to return a moment object.

Then we call tz with the time zone string to set the time zone of the moment object.

And then we call format to return a date time string.

As a result we get '2022-01-17T18:17:02-08:00'.

Conclusion

To use time zone offset in Node.js, we can use the node-time library.

To install it, we run npm i moment-timezone.

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 *