Deepender
Deepender

Reputation: 1

How to format a date with specific time zone in Angular?

I am trying to format the timestamp into a specific format. Please help me to do this using moment.js

required format: 'MM-DD-YYYY HH:mm:ss':'UTC -5' this can be UTC -6 as well. I was able to achieve this using Angular date pipe. But now I want this to be done using the moment.js library.

Kindly help

Upvotes: 0

Views: 1771

Answers (1)

Mystic Groot
Mystic Groot

Reputation: 143

I would Personally use day.js as Moment.js is a legacy project, now in maintenance mode.

To your question it is a bit unclear to understand but this is what I got from reading it. This is one of the many approaches you can take. I think you can install moment-timezone npm install moment-timezone

and the you can use something like this
now = moment(new Date());

currentDate = this.now.tz("asia_yekaterinburg").format("MM-DD-YYYY HH:mm:ss");

I have the working example. https://moment-js-timezone.stackblitz.io

Feel free to improve this answer or modify.

Update using with Moment.js
In the component
import moment = require ("moment");
now = moment(new Date());
momentDate = moment(this.nowTime).utcOffset(-5).format("MM-DD-YYYY HH:mm:ss");

In the Html

{{momentDate}}

Upvotes: 1

Related Questions