Date().timeIntervalSince1970 in javascript

Viewed 473

In my react native app I am setting the date using the following:

new Date().getTime()

In my Swift App im using:

Date().timeIntervalSince1970

Is there a function or something I can use in Javascript which is similar to Date().timeIntervalSince1970 or vice-versa (the data is being stored in firebase cloud storage).

Because im using different formats, messages are not showing in the correct order.

2 Answers

The code timeIntervalSince1970 as per the documentation is the seconds from 1970 to the date.

So a similar function in javascript can be Date.getTime(), but it returns the milliseconds. so you will have to divide that by 1000

new Date().getTime() / 1000

also if you are checking of the reverse, you should be multiplying the seconds with 1000 to get the milliseconds

you can use Date.now() method which returns the number of milliseconds elapsed since January 1, 1970 00:00:00 UTC, then divide by 1000, like this:

const millis = Date.now();

const time = Math.floor(millis / 1000);

console.log(time);

Related