Tell me pls, have a number, for example 1440 and i need get or minute, or minutes, or hour or hours. What can I do? I do:
var getTimeouts = function (timeouts) {
var timeout = +timeouts,
timeoutValue = "";
if (timeout > 0) {
if (timeout % 60 == 0) {
if (timeout == 60) {
// timeoutValue = '1 hour';
} else {
// timeoutValue = '... hours';
}
} else {
if (timeout == 1) {
// timeoutValue = '1 minute';
} else {
// timeoutValue = '... minutes';
}
}
}
return timeoutValue;
};
console.log(getTimeouts(1440)); // 24 hours
Question: how with momont.js I can parse a number and get minute/minutes/hour/hours?
You can only sort of do this with momentjs.
moment.duration().humanize()
will give you a string that loosely represents the duration.
moment.duration(60,"minute").humanize(); //= an hour
moment.duration(1,"minute").humanize(); //= a minute
moment.duration(5,"minute").humanize(); //= 5 minutes
moment.duration(55,"minute").humanize(); //= an hour <-!!!
Depending on you needs, it might be good enough.
But if you need something that is actually accurate, use the HumanizeDuration plugin .
humanizeDuration(3000) // "3 seconds"
humanizeDuration(2015) // "2.25 seconds"
humanizeDuration(97320000) // "1 day, 3 hours, 2 minutes"
And if you don't need the extra language support it comes with, just manually delete those parts of the plugin.
Edit:
In case I misunderstood and you just need a number representing the hours, minutes, seconds, etc.
You can use moment.duration().as('something')
.
var getTimeouts = function (timeouts) {
var timeout = +timeouts,
timeoutValue = "";
if (timeout > 0) {
if (timeout % 60 == 0) {
if (timeout == 60) {
timeoutValue = '1 hour';
} else {
timeoutValue = parseInt(
moment.duration(timeouts, 'minutes').as('hours'), 10) + ' hours';
}
} else {
if (timeout == 1) {
timeoutValue = '1 minute';
} else {
timeoutValue = parseInt(
moment.duration(timeouts, 'minutes').as('minute'), 10) + ' minutes';
}
}
}
return timeoutValue;
};
Working copy: http://jsfiddle.net/slicedtoad/c90v76s9/3/
You could of course add the logic to make it give you stuff like "1 hour, 2 minutes"
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.