[javascript] Get current date/time in seconds

How do I get the current date/time in seconds in Javascript?

This question is related to javascript datetime

The answer is


To get the number of seconds from the Javascript epoch use:

date = new Date();
milliseconds = date.getTime();
seconds = milliseconds / 1000;

 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.)


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");
}

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.

Using new Date().getTime() / 1000 is an incomplete solution for obtaining the seconds, because it produces timestamps with floating-point units.

const timestamp = new Date() / 1000; // 1405792936.933
// Technically, .933 would be milliseconds. 

A better solution would be:

// Rounds the value
const timestamp = Math.round(new Date() / 1000); // 1405792937

// - OR -

// Floors the value
const timestamp = new Date() / 1000 | 0; // 1405792936

Values without floats are also safer for conditional statements, as the float may produce unwanted results. The granularity you obtain with a float may be more than needed.

if (1405792936.993 < 1405792937) // true

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.


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

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. :)


_x000D_
_x000D_
// The Current Unix Timestamp_x000D_
// 1443535752 seconds since Jan 01 1970. (UTC)_x000D_
_x000D_
// Current time in seconds_x000D_
console.log(Math.floor(new Date().valueOf() / 1000));  // 1443535752_x000D_
console.log(Math.floor(Date.now() / 1000));            // 1443535752_x000D_
console.log(Math.floor(new Date().getTime() / 1000));  // 1443535752
_x000D_
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
_x000D_
_x000D_
_x000D_

jQuery

_x000D_
_x000D_
console.log(Math.floor($.now() / 1000));               // 1443535752
_x000D_
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
_x000D_
_x000D_
_x000D_


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.


I use this:

Math.round(Date.now() / 1000)

No need for new object creation (see doc Date.now())


On some day in 2020, inside Chrome 80.0.3987.132, this gives 1584533105

~~(new Date()/1000) // 1584533105
Number.isInteger(~~(new Date()/1000)) // true