var seconds = new Date().getTime() / 1000;
....will give you the seconds since midnight, 1 Jan 1970
Reference
Answer from sje397 on Stack Overflow Top answer 1 of 16
598
var seconds = new Date().getTime() / 1000;
....will give you the seconds since midnight, 1 Jan 1970
Reference
2 of 16
140
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
Which may cause unwanted results in conditional statements:
if (1405792936.993 < 1405792937) // true
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.
Warning: Bitwise operators could be used to truncate floating-point units, but can also cause issues when working with timestamps. For example, the following bitwise expression (new Date() / 1000) | 0, is less preferred than Math.floor(Date.now() / 1000), because it 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, but instead explicitly converted by the developer in code.
- The signed 32 bit integer timestamp produced by the bitwise operator, causes the year 2038 problem as noted in the comments.
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Date › now
Date.now() - JavaScript | MDN
// This example takes 2 seconds to run const start = Date.now(); console.log("starting timer..."); // Expected output: "starting timer..." setTimeout(() => { const ms = Date.now() - start; console.log(`seconds elapsed = ${Math.floor(ms / 1000)}`); // Expected output: "seconds elapsed = 2" }, ...
Videos
W3Schools
w3schools.com › jsreF › jsref_getseconds.asp
JavaScript Date getSeconds() Method
getSeconds() returns the seconds (0 to 59) of a date. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com · If you want to report an error, or if you want to make ...
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Date › getSeconds
Date.prototype.getSeconds() - JavaScript | MDN
July 10, 2025 - The getSeconds() method of Date instances returns the seconds for this date according to local time.
TutorialsPoint
tutorialspoint.com › get-current-date-time-in-seconds-javascript
Get current date/time in seconds - JavaScript?
January 27, 2025 - Then, multiply the hours to 3600 and minutes to 60; rest you can see below − ... <html> <head> <title>JavaScript Get Seconds</title> </head> <body> <script> var dt = new Date(); var sec = dt.getSeconds(); document.write("Seconds: " + sec); var min = dt.getMinutes(); document.write("<br>Minutes: ...
Stack Abuse
stackabuse.com › bytes › how-to-convert-a-date-to-timestamp-in-javascript
How to Convert a Date to Timestamp in JavaScript
October 4, 2023 - In this Byte, we'll explore how to convert a date or date string to a timestamp in JavaScript. But why do we need to convert a date or date string to a timestamp in the first place? Timestamps are a more universal way of storing date and time information. They represent the number of seconds (or ...
Futurestud.io
futurestud.io › tutorials › how-to-get-a-unix-timestamp-in-javascript-or-node-js
How to Get a UNIX Timestamp in JavaScript or Node.js
June 23, 2022 - JavaScript’s global Date object comes with methods to interact with dates and times. The Date.now() function returns the current timestamp in milliseconds. These milliseconds are counted from the UNIX epoch. That means you need to convert these milliseconds to seconds and you’re receiving the UNIX time:
Moment.js
momentjs.com › docs
Moment.js | Docs
This is implemented as moment(timestamp * 1000), so partial seconds in the input timestamp are included. ... Note: Despite Unix timestamps being UTC-based, this function creates a moment object in local mode. If you need UTC, then subsequently call .utc(), as in: ... You can create a Moment ...
Coderwall
coderwall.com › p › rbfl6g › how-to-get-the-correct-unix-timestamp-from-any-date-in-javascript
How to get the correct Unix Timestamp from any Date in JavaScript (Example)
June 26, 2023 - Unfortunately, there's a small ... Date.getTime(): A Unix timestamp is defined as the number of seconds that have elapsed since 00:00:00 Coordinated Universal Time (UTC), Thursday, 1 January 1970....
Readthedocs
momentjscom.readthedocs.io › en › latest › moment › 01-parsing › 08-unix-timestamp
Unix Timestamp (seconds) - momentjs.com
This is implemented as moment(timestamp * 1000), so partial seconds in the input timestamp are included.
30 Seconds of Code
30secondsofcode.org › home › javascript › date › date to unix timestamp
Convert between a JavaScript Date object and a Unix timestamp - 30 seconds of code
January 7, 2024 - const toTimestamp = date => Math.floor(date.getTime() / 1000); const fromTimestamp = timestamp => new Date(timestamp * 1000); toTimestamp(new Date('2024-01-04')); // 1704326400 fromTimestamp(1704326400); // 2024-01-04T00:00:00.000Z ... Quickly and easily determine if a given JavaScript Date object is a weekday or weekend. ... Leverage the Date object to check if a given date is inside business hours.