# 如何获得自Java时代以来的秒数？

2020/11/09 02:42 · javascript ·  · 0评论

``````var seconds = new Date() / 1000;
``````

``````var d = new Date();
var seconds = d.getTime() / 1000;
``````

``````var d = new Date();
var seconds = Math.round(d.getTime() / 1000);
``````

``````new Date().getTime() / 1000
``````

``````function seconds_since_epoch(){ return Math.floor( Date.now() / 1000 ) }
``````

``````foo = seconds_since_epoch();
``````

``````var time_in_millis = Date.now();
var time_in_seconds = time_in_millis / 1000;
``````

``````time_in_seconds_int = time_in_seconds >= 0 ?
Math.floor(time_in_seconds) : Math.ceil(time_in_seconds);
``````

b。如果您只想保留整数除法的数学定义，请发言。（Python的整数除法可以做到这一点）。

``````time_in_seconds_int = Math.floor(time_in_seconds);
``````

``````var seconds = Math.floor(new Date() / 1000);
``````

``````var ms = new Date().getTime();
``````

``````var sec = new Date().getTime() / 1000;
``````

``````var msEpoch = (+new Date());
var sEpoch = (+new Date()) / 1000;
``````

``````Math.floor(Date.now() / 1000)
``````

``````var date = new Date().getTime()
console.debug('date: ' + date);

if (Date.now() < date)
console.debug('ko');
else
console.debug('ok');
``````

https://www.eovao.com/cn/a/javascript%20date/1/how-to-obtain-current-date-in-milliseconds-by-javascript-（纪元）

``````EPOCH means time from 01 January 1970
var date = new Date();
Following line will return the number of milliseconds from 01 Jaunary 1970
var ms = date.getTime();
Following line will convert milliseconds to seconds
var seconds = Math.floor(ms/1000);
console.log("Seconds since epoch =",seconds);
``````