How to format moment date without considering user's timezone?

62 views Asked by At

Scenario: A user selects a date from +01:00 timezone as 2023-08-26. But when another user from +09:00 timezone view the date it displays as 2023-08-27.

I have a date pipe to format dates into M/D/YYYY format. Value is passing to my dateFormat pipe as a moment object like below.

enter image description here

Is there a way to get the date without converting it to user's timezone?

What I tried:

const extractedDate = moment(value).toDate();

But if i use above method value is getting converted to user's local timezone.

1

There are 1 answers

0
Tim Stack On

If you are using moment-timezone, you can set the default timezone so that date objects are always in the same timezone.

const moment = require("moment-timezone")
moment.tz.setDefault("UTC");

const epochDate = 1692979660;
const extractedDate = moment(epochDate, "X").toDate();