我需要用一个整数来分割时间(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().
更多推荐
发布评论