以MM:SS格式将秒转换为分秒
Convert seconds to minutes and seconds in MM:SS format
我想以 MM:SS 格式将秒转换为分钟和秒。下面的代码工作正常,但将秒数转换为 M:SS 格式。如何将其更改为 MM:SS 格式。
例如,当将 150 seconds
传递给 format(150)
时给出 2:30
但我需要它 02:30
function format(time) {
// Hours, minutes and seconds
var hrs = ~~(time / 3600);
var mins = ~~((time % 3600) / 60);
var secs = ~~time % 60;
// Output like "1:01" or "4:03:59" or "123:03:59"
var ret = "";
if (hrs > 0) {
ret += "" + hrs + ":" + (mins < 10 ? "0" : "");
}
ret += "" + mins + ":" + (secs < 10 ? "0" : "");
ret += "" + secs;
return ret;
}
您可以将 padStart
用于 min
ret += "" + String(mins).padStart(2, '0') + ":" + (secs < 10 ? "0" : "");
function format(time) {
// Hours, minutes and seconds
var hrs = ~~(time / 3600);
var mins = ~~((time % 3600) / 60);
var secs = ~~time % 60;
// Output like "1:01" or "4:03:59" or "123:03:59"
var ret = "";
if (hrs > 0) {
ret += "" + hrs + ":" + (mins < 10 ? "0" : "");
}
ret += "" + String(mins).padStart(2, '0') + ":" + (secs < 10 ? "0" : "");
ret += "" + secs;
return ret;
}
console.log(format(150))
你可以用这个
ret = (hrs>0?("" + hrs + ":"):"") + (mins < 10 ? "0"+mins : mins) + ":" + (secs < 10 ? "0"+secs : secs);
让我们试试:
ret = (hrs ? hrs + ":" : '') + ("0" + mins).slice(-2) + ":" + ("0" + secs).slice(-2)
祝你好运:D
我想以 MM:SS 格式将秒转换为分钟和秒。下面的代码工作正常,但将秒数转换为 M:SS 格式。如何将其更改为 MM:SS 格式。
例如,当将 150 seconds
传递给 format(150)
时给出 2:30
但我需要它 02:30
function format(time) {
// Hours, minutes and seconds
var hrs = ~~(time / 3600);
var mins = ~~((time % 3600) / 60);
var secs = ~~time % 60;
// Output like "1:01" or "4:03:59" or "123:03:59"
var ret = "";
if (hrs > 0) {
ret += "" + hrs + ":" + (mins < 10 ? "0" : "");
}
ret += "" + mins + ":" + (secs < 10 ? "0" : "");
ret += "" + secs;
return ret;
}
您可以将 padStart
用于 min
ret += "" + String(mins).padStart(2, '0') + ":" + (secs < 10 ? "0" : "");
function format(time) {
// Hours, minutes and seconds
var hrs = ~~(time / 3600);
var mins = ~~((time % 3600) / 60);
var secs = ~~time % 60;
// Output like "1:01" or "4:03:59" or "123:03:59"
var ret = "";
if (hrs > 0) {
ret += "" + hrs + ":" + (mins < 10 ? "0" : "");
}
ret += "" + String(mins).padStart(2, '0') + ":" + (secs < 10 ? "0" : "");
ret += "" + secs;
return ret;
}
console.log(format(150))
你可以用这个
ret = (hrs>0?("" + hrs + ":"):"") + (mins < 10 ? "0"+mins : mins) + ":" + (secs < 10 ? "0"+secs : secs);
让我们试试:
ret = (hrs ? hrs + ":" : '') + ("0" + mins).slice(-2) + ":" + ("0" + secs).slice(-2)
祝你好运:D