我想转换时间的持续时间,即秒数,以冒号分隔的时间字符串(hh:mm:ss)
我在这里找到了一些有用的答案,但它们都谈到了转换成x小时和x分钟的格式。
那么有一个小片段,这是在jQuery或只是原始JavaScript?
我想转换时间的持续时间,即秒数,以冒号分隔的时间字符串(hh:mm:ss)
我在这里找到了一些有用的答案,但它们都谈到了转换成x小时和x分钟的格式。
那么有一个小片段,这是在jQuery或只是原始JavaScript?
当前回答
function secondsToTime(secs)
{
var hours = Math.floor(secs / (60 * 60));
var divisor_for_minutes = secs % (60 * 60);
var minutes = Math.floor(divisor_for_minutes / 60);
var divisor_for_seconds = divisor_for_minutes % 60;
var seconds = Math.ceil(divisor_for_seconds);
if(hours >= 12)
{
var m= 'pm' ;
}
else
{
var m='am'
}
if(hours-12 >0)
{
var hrs = hours-12;
}
else if(hours-12 <0)
{
var hrs = hours;
}
var obj = {
"h": hrs,
"m": minutes,
"s": seconds,
"a":m
};
return obj;
}
var d = new Date();
var n = d.getHours();
var hms = d.getHours()+':'+d.getMinutes()+':'+d.getSeconds(); // your input string
var a = hms.split(':'); // split it at the colons
// minutes are worth 60 seconds. Hours are worth 60 minutes.
var seconds = (+a[0]) * 60 * 60 + (+a[1]) * 60 + (+a[2]);
console.log(seconds);
console.log(secondsToTime(seconds))
https://jsfiddle.net/jithinksoft/9x6z4sdt/
其他回答
正则表达式可以用来匹配Date对象的toString()方法返回的字符串中的时间子字符串,该字符串的格式如下:“Thu Jul 05 2012 02:45:12 GMT+0100 (GMT夏令时)”。请注意,此解决方案使用自epoch以来的时间:1970年1月1日午夜。这个解决方案可以是一行代码,不过将其拆分会更容易理解。
function secondsToTime(seconds) {
const start = new Date(1970, 1, 1, 0, 0, 0, 0).getTime();
const end = new Date(1970, 1, 1, 0, 0, parseInt(seconds), 0).getTime();
const duration = end - start;
return new Date(duration).toString().replace(/.*(\d{2}:\d{2}:\d{2}).*/, "$1");
}
要获得格式为hh:MM:ss的时间部分,可以使用下面的正则表达式:
(上面有人在同一篇文章中提到了这一点,谢谢。)
var替换= new日期().toTimeString () .replace (/ . * (\ d {2}: \ d {2}: \ d{2})。* / " $ 1 "); console.log(替换)
function secondsToTime(secs)
{
var hours = Math.floor(secs / (60 * 60));
var divisor_for_minutes = secs % (60 * 60);
var minutes = Math.floor(divisor_for_minutes / 60);
var divisor_for_seconds = divisor_for_minutes % 60;
var seconds = Math.ceil(divisor_for_seconds);
if(hours >= 12)
{
var m= 'pm' ;
}
else
{
var m='am'
}
if(hours-12 >0)
{
var hrs = hours-12;
}
else if(hours-12 <0)
{
var hrs = hours;
}
var obj = {
"h": hrs,
"m": minutes,
"s": seconds,
"a":m
};
return obj;
}
var d = new Date();
var n = d.getHours();
var hms = d.getHours()+':'+d.getMinutes()+':'+d.getSeconds(); // your input string
var a = hms.split(':'); // split it at the colons
// minutes are worth 60 seconds. Hours are worth 60 minutes.
var seconds = (+a[0]) * 60 * 60 + (+a[1]) * 60 + (+a[2]);
console.log(seconds);
console.log(secondsToTime(seconds))
https://jsfiddle.net/jithinksoft/9x6z4sdt/
我不喜欢在JavaScript中为标准数据类型添加属性,所以我建议这样做:
/**
* Format a duration in seconds to a human readable format using the notion
* "h+:mm:ss", e.g. "4:40:78". Negative durations are preceeded by "-".
*
* @param t Duration in seconds
* @return The formatted duration string
*/
var readableDuration = (function() {
// Each unit is an object with a suffix s and divisor d
var units = [
{s: '', d: 1}, // Seconds
{s: ':', d: 60}, // Minutes
{s: ':', d: 60}, // Hours
];
// Closure function
return function(t) {
t = parseInt(t); // In order to use modulus
var trunc, n = Math.abs(t), i, out = []; // out: list of strings to concat
for (i = 0; i < units.length; i++) {
n = Math.floor(n / units[i].d); // Total number of this unit
// Truncate e.g. 26h to 2h using modulus with next unit divisor
if (i+1 < units.length) // Tweak substr with two digits
trunc = ('00'+ n % units[i+1].d).substr(-2, 2); // …if not final unit
else
trunc = n;
out.unshift(''+ trunc + units[i].s); // Output
}
(t < 0) ? out.unshift('-') : null; // Handle negative durations
return out.join('');
};
})();
用法:
var str = readableDuration(3808); // "1:03:28"
我还创建了一个更通用的版本。主要的区别是它接受毫秒(这是JS中的标准时间单位),而输出格式使用空格。
function secToTime(seconds, separator) {
return [
parseInt(seconds / 60 / 60),
parseInt(seconds / 60 % 60),
parseInt(seconds % 60)
].join(separator ? separator : ':')
.replace(/\b(\d)\b/g, "0$1").replace(/^00\:/,'')
}
你现在可以这样使用它:
alert(secToTime("123"));
工作代码片段:
函数secToTime(秒,分隔符){ 返回( parseInt(seconds / 60 / 60), parseInt(seconds / 60% 60), parseInt(seconds % 60) ]。加入(分离器?分隔符:':') .replace (/ \ b \ b / g (\ d),“0 1美元”).replace(/ ^ 00 \: /,”) } console.log (secToTime (" 123 "));