Timestamp in milliseconds
The timestamp is milliseconds since January 1, 1970 in UTC. The
Date.now() function in JS returns the timestamp of the current time.
Timestamp from a date string
- Create a new
Dateobject from a date string by using the constructor.
- Call the
getTime()function on the
Dateobject to get the timestamp in milliseconds.
- Divide the result by
1000and round it down to get a Unix timestamp.
valueOf() functions do the exact same thing and can be used interchangeably.
privacy.resistFingerprinting, your result may be rounded, and not come in milliseconds. If that can cause a problem for you, consider implementing your own checks for that. Read more on MDN.
The Unix timestamp is in seconds rather than milliseconds. Simply divide the timestamp by 1000:
Incidentally, date-fns has functions to get the timestamps, both regular and Unix. In addition, it has functions to format and localize dates, and do date maths.
You can find the code examples in my GitHub repo.