Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Getting a Unix Timestamp for a future date and time in JavaScript

How can I get a future unix timestamp in Javacript? ie, pass "2023-03-08 13:05:00" and get a unix timestamp corresponding to that time.

All the examples I see use the getTime() function which is the current time like this:
Javascript: future time as UNIX timestamp

I found a solution in Php which I think serves the purpose I am looking for:
Getting a future unix timestamp using a specific date and time?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Is there a similar function in Javascript?

>Solution :

// Create a Date object for the future time

var futureTime = new Date("2023-03-08 13:05:00");

// Get the Unix timestamp (in milliseconds) for the future time

var futureTimestamp = futureTime.getTime();

// Convert milliseconds to seconds

var futureUnixTimestamp = Math.floor(futureTimestamp / 1000);

console.log(futureUnixTimestamp); // Output: 1678385100
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading