What does a negative Unix timestamp mean?
A date before January 1, 1970. For example, -86400 is December 31, 1969.
Convert Unix timestamp to date and vice versa
Unix was developed in 1969-1970, and the developers chose January 1, 1970 as a convenient starting point.
A date before January 1, 1970. For example, -86400 is December 31, 1969.
Use Date.now() for milliseconds or Math.floor(Date.now()/1000) for seconds.
A Unix timestamp is the number of seconds (or milliseconds) that have elapsed since January 1, 1970, 00:00:00 UTC -- known as the Unix epoch. It's the universal way computers store time: a single integer that's unambiguous, timezone-independent, and easy to do math with.
A 10-digit number is a Unix timestamp in seconds (e.g., 1700000000). A 13-digit number is milliseconds (e.g., 1700000000000). JavaScript's Date.now() returns milliseconds; most Unix/Linux system calls use seconds. When an API gives you a timestamp, count the digits to figure out which one you're dealing with.
Storing dates as strings creates headaches: Which format? MM/DD/YYYY or DD/MM/YYYY or YYYY-MM-DD? What timezone? When you sort them alphabetically, do they sort chronologically? Timestamps solve all of this. They're just numbers -- trivially sortable, comparable with a simple subtraction, and timezone-neutral. A timestamp of 1700000000 means the same moment in every country on Earth.
Unix timestamps are always UTC. If you're in UTC+9 (Korea), you add 9 hours (32,400 seconds) to convert to local time. The golden rule for any system that handles time: store in UTC, display in local time. Convert as late as possible -- only at the point of display. This prevents bugs where different servers in different timezones disagree about what time it is.
In 2038, 32-bit Unix timestamps will overflow. On January 19, 2038 at 03:14:07 UTC, a 32-bit signed integer can't go any higher -- it wraps around to a large negative number. 64-bit systems handle timestamps up to the year 292 billion, so modern systems are fine. But some legacy embedded systems and old databases still use 32-bit timestamps.