如何使用moment.js将分钟转换为小时

问题描述:

任何人都可以告诉我如何使用moment.js将分钟转换为小时,并以hh:mm A格式显示。

Can anyone tell me how to convert minutes to hours using moment.js and display in hh:mm A format.

例如,
如果分钟是480它应该显示输出为08:00 AM。
如果分钟是1080,它应该显示输出为06:00 PM

For example, If minutes is 480 it should display output as 08:00 AM. If minutes is 1080 it should display output as 06:00 PM

你可以像基本算术那样做所以:

You can just do the basic arithmetic like so:

function getTimeFromMins(mins) {
    // do not include the first validation check if you want, for example,
    // getTimeFromMins(1530) to equal getTimeFromMins(90) (i.e. mins rollover)
    if (mins >= 24 * 60 || mins < 0) {
        throw new RangeError("Valid input should be greater than or equal to 0 and less than 1440.");
    }
    var h = mins / 60 | 0,
        m = mins % 60 | 0;
    return moment.utc().hours(h).minutes(m).format("hh:mm A");
}


getTimeFromMins(480); // returns "08:00 AM"
getTimeFromMins(520); // returns "08:40 AM"
getTimeFromMins(1080); // returns "06:00 PM"