用Javascript中的整数除hh:mm:ss(Dividing hh:mm:ss by an integer in Javascript)

编程入门 行业动态 更新时间:2024-10-24 20:22:50
用Javascript中的整数除hh:mm:ss(Dividing hh:mm:ss by an integer in Javascript)

我需要用一个整数来分割时间(hh:mm:ss)。 例如13:14:24 / 12 。

如果我将它转换为日期和分割:

new Date( date.getMonth()+1 + " " + date.getDate() + ", " + date.getFullYear() + " " + "13:14:24") / 12;

我得到一个非常长的数字119579922000 ,它是以毫秒为单位的日期/ 12吗? 我需要的结果是相同的hh:mm:ss格式。

I need to split time (hh:mm:ss) by an integer. For example 13:14:24 / 12.

If I convert it into date and divide:

new Date( date.getMonth()+1 + " " + date.getDate() + ", " + date.getFullYear() + " " + "13:14:24") / 12;

I get a really long number 119579922000, is it date / 12 in milliseconds? I need the result to be in the same hh:mm:ss format.

最满意答案

var h = 13, m = 14, s = 24; var secsSinceMidnight = (h*3600) + (m*60) + s; var oneTwelth = secsSinceMidnight / 12; h = Math.floor(oneTwelth / 3600); m = Math.floor( (oneTwelth % 3600) / 60); s = Math.floor( (oneTwelth % 3600) % 60); console.log(h + ":" + m + ":" + s);

下面是使用Sugar.js库的另一种方法,这是我在JavaScript中用于日期处理的个人选择扩展:

var midnight = Date.create().beginningOfDay(); var secsSinceMidnight = Date.create().secondsSince(midnight); console.log( (secsSinceMidnight/12).secondsAfter(midnight) );

解释最后一行: secondsAfter是Number类型上定义的函数。 它返回一个Date对象,然后发送给console.log() 。

var h = 13, m = 14, s = 24; var secsSinceMidnight = (h*3600) + (m*60) + s; var oneTwelth = secsSinceMidnight / 12; h = Math.floor(oneTwelth / 3600); m = Math.floor( (oneTwelth % 3600) / 60); s = Math.floor( (oneTwelth % 3600) % 60); console.log(h + ":" + m + ":" + s);

Here is an alternative approach using the Sugar.js library, which is my personal choice extension for date handling in JavaScript:

var midnight = Date.create().beginningOfDay(); var secsSinceMidnight = Date.create().secondsSince(midnight); console.log( (secsSinceMidnight/12).secondsAfter(midnight) );

To explain the last line: secondsAfter is a function defined on the Number type. It is returning a Date object, which is then sent to console.log().

更多推荐

本文发布于:2023-07-30 22:48:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1340322.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:整数   hh   Javascript   mm   integer

发布评论

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

>www.elefans.com

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