将秒转换为时间格式 [英] Converting Seconds to Time Format

查看:127
本文介绍了将秒转换为时间格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找到一种将秒转换为时间格式的好方法.

I am trying to find a good solution for converting seconds to time format.

我有此功能,到目前为止可以满足我的需求.

I have this function which works fine for my needs so far.

function secondstotime(secs)
{
    var t = new Date(1970,0,1);
    t.setSeconds(secs);
    var s = t.toTimeString().substr(0,8);
    if(secs > 86399)
        s = Math.floor((t - Date.parse("1/1/70")) / 3600000) + s.substr(2);
    return s;
}

alert(secondstotime(1920));

因此您可以在jsfiddle中运行它 http://jsfiddle.net/7Pp5z/

So you can run this in jsfiddle http://jsfiddle.net/7Pp5z/

所以这很好用,可以工作数小时等等,但是我希望在时间的左边去除零.举个例子

So this works great and works for hours etc but i am looking to strip the zeros to the left of the time. Taking the example

我要00:32:00

i want 00:32:00

变成32:00,这样在输出到浏览器时看起来会更好

to become 32:00 so it looks better that way when outputted to the browser

有人可以告诉我执行此操作的最佳方法吗,或者有人可以共享其他功能吗?

Can someone tell me the best way to do this or does anyone have another function they could possibly share.

谢谢

推荐答案

输入以下条件:

if(s.substr(0, 2) == 00)
        return s.substr(3);

工作演示 http://jsfiddle.net/7Pp5z/2/

这篇关于将秒转换为时间格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆