Ashy Ashcsi
Ashy Ashcsi

Reputation: 1597

moment.utc('date string').format('DD-MMM-YYYY') returns the previous date

The Date in my database is getting stored as: '31-MAR-20'. I am using the following code to display the date back on UI.

moment.utc(data['dateString']).format('DD-MMM-YYYY');

But the above code is displaying the previous date i.e. 30-Mar-2020. How do I correct this issue ? Can I do it without using moment.js.

Thanks

Upvotes: 1

Views: 493

Answers (1)

segFault
segFault

Reputation: 4054

You could attempt to use the default moment() for local mode.

From those docs:

moment(...) is local mode. Ambiguous input (without offset) is assumed to be local time. Unambiguous input (with offset) is adjusted to local time.

Example:

moment(data['dateString']).format('DD-MMM-YYYY');

NOTE: depending on your sites use case (especially if you plan to have global visitors), it may be a good idea to store the date in UTC for translation purposes like this. To ensure everyone sees the date relative to their location.

Upvotes: 1

Related Questions