Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get current date/time in seconds

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

like image 977
Hailwood Avatar asked Sep 30 '10 11:09

Hailwood


People also ask

How can I get seconds of current date?

To get the current date and time in seconds: Copied! const date = new Date(); const dateTimeInSeconds = Math. floor(date. getTime() / 1000); // 👇️ 164328461 console.

How do I print time in seconds in Python?

Use the time. time() function to get the current time in seconds since the epoch as a floating-point number. This method returns the current timestamp in a floating-point number that represents the number of seconds since Jan 1, 1970, 00:00:00. It returns the current time in seconds.

Is new date () getTime () UTC?

Use the getTime() method to get a UTC timestamp, e.g. new Date(). getTime() . The method returns the number of milliseconds since the Unix Epoch and always uses UTC for time representation. Calling the method from any time zone returns the same UTC timestamp.


3 Answers

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

....will give you the seconds since midnight, 1 Jan 1970

Reference

like image 117
sje397 Avatar answered Oct 09 '22 18:10

sje397


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

like image 126
Konstantin Schubert Avatar answered Oct 09 '22 19:10

Konstantin Schubert


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:

  1. 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.
  2. The signed 32 bit integer timestamp produced by the bitwise operator, causes the year 2038 problem as noted in the comments.
like image 117
tim-montague Avatar answered Oct 09 '22 20:10

tim-montague