How to resolve loss of minute precision formatting a moment in 12HR?

拟墨画扇 提交于 2019-12-25 07:23:48

问题


I've converted an ISO date string to a momentjs moment and then formatted that moment using .format("MM/DD/YYYY HH:MM").

When I output the final formatted moment, the minute value is incorrect as against the value read back from the iso string originally.

In this case the ISO string value holds 3:10 PM or "2016-08-03T03:10:00.000Z" as represented in the string. But when I call format the moment value is 4:07PM meaning three minutes have been subtracted during the format.

During debug I noted the following values at each assignment stage:

Step 1 (converting the db value to an ISO string):

var actualBCR_Local = moment.utc('@Model.Escalation.Actual_BCR_ISO').toISOString();

value: "2016-08-03T03:10:00.000Z"

Step 2 (converting the ISO string to a momentjs moment in order to represent the local time GMT+1):

 var actualBCR_Local_Moment = moment(actualBCR_Local);

 value: Wed Aug 03 2016 04:10:00 GMT+0100 (GMT Daylight Time)

Step 3 (formatting the moment in 12HR format for presentation, issue is here as I lose 3 minutes as against the original value which should be 04:10):

 var actualBCR_Local_Formatted = actualBCR_Local_Moment.format("MM/DD/YYYY HH:MM"); 

 value: "08/03/2016 04:08"

How can I prevent loss of minute precision when formatting a moment in 12HR format?


回答1:


that's because you use the wrong format

In this section, you will find out that you are using HH:MM which means hour:month

If you use HH:mm then you will get correct time.

and that's why you got 2 minute loss, cause it display 08 as "month"

here is the what I tested



来源:https://stackoverflow.com/questions/38679715/how-to-resolve-loss-of-minute-precision-formatting-a-moment-in-12hr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!