简体   繁体   中英

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 ie 30-Mar-2020. How do I correct this issue ? Can I do it without using moment.js.

Thanks

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM