将秒转换为时间格式

编程入门 行业动态 更新时间:2024-10-24 01:59:18
本文介绍了将秒转换为时间格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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中运行它 jsfiddle/7Pp5z/

So you can run this in jsfiddle jsfiddle/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);

工作演示 jsfiddle/7Pp5z/2/

更多推荐

将秒转换为时间格式

本文发布于:2023-10-16 12:11:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1497516.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:转换为   格式   时间

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!