javascript .getTime() 替代没有毫秒
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29079575/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
.getTime() Alternative without milliseconds
提问by DomX23
Is there a way to use the Date().getTime() function without getting the time in milliseconds? If not is there a substitute for .getTime() which will only give me the precision in minutes?
有没有办法使用 Date().getTime() 函数而不以毫秒为单位获取时间?如果没有,是否有 .getTime() 的替代品,它只会给我几分钟的精度?
Also I'm not sure how to just strip the milliseconds out of the date object.
另外我不确定如何从日期对象中去除毫秒。
var time = new Date().getTime()
Output: 1426515375925
回答by James Donnelly
Simple arithmetic. If you want the value in seconds, divide the milliseconds result by 1000:
简单的算术。如果您想要以秒为单位的值,请将毫秒结果除以 1000:
var seconds = new Date().getTime() / 1000;
You might want to call Math.floor()
on that to remove any decimals though:
你可能想调用Math.floor()
它来删除任何小数:
var seconds = Math.floor(new Date().getTime() / 1000);
Is there a different function I can use that will give me only the minutes since 1/1/1970, I just dont want the number to be as precise.
有没有我可以使用的不同功能,它只会给我自 1970 年 1 月 1 日以来的分钟数,我只是不希望数字如此精确。
Certainly, divide the seconds by 60, or divide the milliseconds by 60000:
当然,将秒除以 60,或者将毫秒除以 60000:
var minutes = Math.floor(new Date().getTime() / 60000);
var milliseconds = 1426515375925,
seconds = Math.floor(milliseconds / 1000), // 1426515375
minutes = Math.floor(milliseconds / 60000); // 23775256
回答by Marek
In my case (date without miliseconds), I wanted miliseconds to always be zero, so it would be like:
在我的情况下(没有毫秒的日期),我希望毫秒始终为零,所以它会像:
hh:mm:ss:000
时:分:秒:000
Here is how I achived it:
这是我实现它的方法:
var time = new Date().getTime();
// Make miliseconds = 0 (precision full seconds)
time -= time % 1000;
Maybe it will be useful for someone
也许它对某人有用
回答by SnelleFiets
The simplest way to remove the milliseconds:
删除毫秒的最简单方法:
Long date = new Date().getTime() / 1000 * 1000
1582302824091L becomes 1582302824000L
2020-02-21 17:33:44.091becomes 2020-02-21 17:33:44.0
1582302824091L 变成1582302824000L
2020-02-21 17:33:44.091变成2020-02-21 17:33:44.0
回答by David Laberge
Here a quick way to remove the milisecont from the getTime
这里有一个快速的方法来从 getTime
var milli = new Date().getTime()
var timeWithoutMilli = Math.floor(milli/1000);
That will return the number of seconds
这将返回秒数
回答by inorganik
Date object of the current time:
当前时间的日期对象:
var time = new Date();
回答by Darkmouse
By default, the time is set to the number of miliseconds since the epoch. To get the number of seconds, simply divide that number by 1000, and round to get a whole number
默认情况下,时间设置为自纪元以来的毫秒数。要获得秒数,只需将该数字除以 1000,然后四舍五入得到一个整数
Math.round(new Date().getTime()/1000)
To get the number of minutes divide that number by 60
要得到分钟数除以 60
Math.round(new Date().getTime()/1000/60)
回答by 9Deuce
You can pull each portion of the time you want and put in a formatted string, like
你可以拉出你想要的每一部分时间并放入一个格式化的字符串,比如
var time = new Date().getHours() + ":" + new Date().getMinutes() + ":" + new Date().getSeconds();