How to get the current date or/and time in seconds
JavascriptDatetimeJavascript Problem Overview
How do I get the current date or/and time in seconds using Javascript?
Javascript Solutions
Solution 1 - Javascript
var seconds = new Date().getTime() / 1000;
....will give you the seconds since midnight, 1 Jan 1970
Solution 2 - Javascript
Date.now()
gives milliseconds since epoch. No need to use new
.
Check out the reference here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/now
(Not supported in IE8.)
Solution 3 - Javascript
Using new Date().getTime() / 1000
is an incomplete solution for obtaining the seconds, because it produces timestamps with floating-point units.
new Date() / 1000; // 1405792936.933
// Technically, .933 would be in milliseconds
Instead use:
Math.round(Date.now() / 1000); // 1405792937
// Or
Math.floor(Date.now() / 1000); // 1405792936
// Or
Math.ceil(Date.now() / 1000); // 1405792937
// Note: In general, I recommend `Math.round()`,
// but there are use cases where
// `Math.floor()` and `Math.ceil()`
// might be better suited.
Also, values without floats are safer for conditional statements, because the granularity you obtain with floats may cause unwanted results. For example:
if (1405792936.993 < 1405792937) // true
Warning: Bitwise operators can cause issues when used to manipulate timestamps. For example, (new Date() / 1000) | 0
can also be used to "floor" the value into seconds, however that code causes the following issues:
- By default Javascript numbers are type 64 bit (double precision) floats, and bitwise operators implicitly convert that type into signed 32 bit integers. Arguably, the type should not be implicitly converted by the compiler, and instead the developer should make the conversion where needed.
- The signed 32 bit integer timestamp produced by the bitwise operator, causes the year 2038 problem as noted in the comments.
Solution 4 - Javascript
Based on your comment, I think you're looking for something like this:
var timeout = new Date().getTime() + 15*60*1000; //add 15 minutes;
Then in your check, you're checking:
if(new Date().getTime() > timeout) {
alert("Session has expired");
}
Solution 5 - Javascript
To get the number of seconds from the Javascript epoch use:
date = new Date();
milliseconds = date.getTime();
seconds = milliseconds / 1000;
Solution 6 - Javascript
// The Current Unix Timestamp
// 1443535752 seconds since Jan 01 1970. (UTC)
// Current time in seconds
console.log(Math.floor(new Date().valueOf() / 1000)); // 1443535752
console.log(Math.floor(Date.now() / 1000)); // 1443535752
console.log(Math.floor(new Date().getTime() / 1000)); // 1443535752
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
jQuery
console.log(Math.floor($.now() / 1000)); // 1443535752
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Solution 7 - Javascript
Solution 8 - Javascript
These JavaScript solutions give you the milliseconds or the seconds since the midnight, January 1st, 1970.
The IE 9+ solution(IE 8 or the older version doesn't support this.):
var timestampInMilliseconds = Date.now();
var timestampInSeconds = Date.now() / 1000; // A float value; not an integer.
timestampInSeconds = Math.floor(Date.now() / 1000); // Floor it to get the seconds.
timestampInSeconds = Date.now() / 1000 | 0; // Also you can do floor it like this.
timestampInSeconds = Math.round(Date.now() / 1000); // Round it to get the seconds.
To get more information about Date.now()
: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/now
The generic solution:
// ‘+’ operator makes the operand numeric.
// And ‘new’ operator can be used without the arguments ‘(……)’.
var timestampInMilliseconds = +new Date;
var timestampInSeconds = +new Date / 1000; // A float value; not an intger.
timestampInSeconds = Math.floor(+new Date / 1000); // Floor it to get the seconds.
timestampInSeconds = +new Date / 1000 | 0; // Also you can do floor it like this.
timestampInSeconds = Math.round(+new Date / 1000); // Round it to get the seconds.
Be careful to use, if you don't want something like this case.
if(1000000 < Math.round(1000000.2)) // false.
Solution 9 - Javascript
Date.now()-Math.floor(Date.now()/1000/60/60/24)*24*60*60*1000
This should give you the milliseconds from the beginning of the day.
(Date.now()-Math.floor(Date.now()/1000/60/60/24)*24*60*60*1000)/1000
This should give you seconds.
(Date.now()-(Date.now()/1000/60/60/24|0)*24*60*60*1000)/1000
Same as previous except uses a bitwise operator to floor the amount of days.
Solution 10 - Javascript
You can met another way to get time in seconds/milliseconds since 1 Jan 1970:
var milliseconds = +new Date;
var seconds = milliseconds / 1000;
But be careful with such approach, cause it might be tricky to read and understand it.
Solution 11 - Javascript
Better short cuts:
+new Date # Milliseconds since Linux epoch
+new Date / 1000 # Seconds since Linux epoch
Math.round(+new Date / 1000) #Seconds without decimals since Linux epoch
Solution 12 - Javascript
On some day in 2020, inside Chrome 80.0.3987.132, this gives 1584533105
~~(new Date()/1000) // 1584533105
Number.isInteger(~~(new Date()/1000)) // true
Solution 13 - Javascript
To get today's total seconds of the day:
getTodaysTotalSeconds(){
let date = new Date();
return +(date.getHours() * 60 * 60) + (date.getMinutes() * 60) + date.getSeconds();
}
I have add +
in return which return in int
. This may help to other developers. :)
Solution 14 - Javascript
if you simply need seconds in THREE JS, use one of the code bellow in function uses window.requestAnimationFrame()
let sec = parseInt(Date.now().toString()[10]); console.log(' counting Seconds => '+ sec );
or
let currentTime= Date.now();
let secAsString= time.toString()[10];
let sec = parseInt(t);
console.log('counting Seconds =>'+ sec );
Solution 15 - Javascript
There is no need to initialize a variable to contain the Date object due to the fact the Date.now()
is a static method which means that is accessible directly from an API object's constructor.
So you can just do this
document.write(Date.now()) // milliseconds
document.write(Date.now()/1000) // seconds
Something fun
Live update of seconds since since January 1, 1970 00:00:00 UTC
let element = document.getElementById('root')
const interval = setInterval(() => {
let seconds = Math.round(Date.now()/1000)
element.innerHTML = seconds
},1000)
Seconds since January 1, 1970 00:00:00 UTC
<h1 id='root'></h1>