Handling time is a fundamental skill in web development, and JavaScript's Date
object provides powerful methods for working with the Unix timestamp. Whether you're building a real-time application, logging events, or syncing data, understanding how to manage timestamps is essential.
This guide covers everything you need: how to get the current Unix timestamp, how to convert a datetime to a timestamp, and how to convert a timestamp to a datetime. We'll also introduce a free Unix timestamp converter for easy validation.
JavaScript Timestamps: Seconds vs. Milliseconds
First, it's important to understand that while the standard Unix timestamp is measured in seconds, JavaScript works natively with milliseconds.
- Seconds Timestamp (10-digit): The universal standard (e.g.,
1754968077
). - Milliseconds Timestamp (13-digit): JavaScript's default (e.g.,
1754968077123
). This is the value returned by mostDate
methods.
Forgetting this distinction is a common source of bugs, so always be mindful of whether you need to multiply or divide by 1000.
How to Get the Current Unix Timestamp
Getting the current timestamp in JavaScript is straightforward using the static Date.now()
method.
Get Timestamp in Milliseconds
This is the most direct method and returns a 13-digit timestamp.
const timestampInMilliseconds = Date.now();
console.log(timestampInMilliseconds);
// Example Output: 1754968077123
Get Timestamp in Seconds
To get the standard 10-digit Unix timestamp, simply divide the result of Date.now()
by 1000 and remove the decimal part.
const timestampInSeconds = Math.floor(Date.now() / 1000);
console.log(timestampInSeconds);
// Example Output: 1754968077
How to Convert Between Datetime and Unix Timestamp
JavaScript's Date
object is the key to converting between human-readable dates and numeric timestamps.
Convert Datetime to Timestamp
To convert a datetime to a timestamp, first create a Date object from a date
string, then use the .getTime()
method to extract the millisecond timestamp.
// Create a new Date object from a string
const myDate = new Date('2025-08-10T00:00:00Z'); // 'Z' denotes UTC
// .getTime() returns the timestamp in milliseconds
const timestampInMilliseconds = myDate.getTime();
console.log(timestampInMilliseconds);
// Output: 1754899200000
// For a standard Unix timestamp, divide by 1000
const timestampInSeconds = timestampInMilliseconds / 1000;
console.log(timestampInSeconds);
// Output: 1754899200
Convert Timestamp to Datetime
To convert a timestamp to a datetime, create a new Date
object by passing the numeric timestamp directly into the constructor. Remember that JavaScript expects milliseconds.
const timestampInMilliseconds = 1754899200000;
// Pass the millisecond timestamp into the Date constructor
const myDate = new Date(timestampInMilliseconds);
// The .toString() method shows the human-readable date in the browser's local timezone
console.log(myDate.toString());
// Example Output: "Sun Aug 10 2025 08:00:00 GMT+0800 (Taipei Standard Time)"
// .toUTCString() shows the UTC version
console.log(myDate.toUTCString());
// Output: "Sun, 10 Aug 2025 00:00:00 GMT"
Validating with a Unix Timestamp Converter
When debugging or testing, you often need to quickly check a timestamp's value. A reliable online Unix timestamp converter is invaluable for this.
The DevUtils Unix Timestamp Converter is a free tool perfect for JavaScript developers:
-
Instantly verify the date and time of any Unix timestamp you generate.
-
Easily convert a date to a timestamp to get the correct number for your tests.
-
Generate timestamps in other useful formats, like for Discord.
-
Work securely with client-side processing, meaning your data never leaves your browser.
JavaScript provides all the tools you need to handle any Unix timestamp. By using the Date object correctly and leveraging a fast Unix timestamp converter like DevUtils for validation, you can manage time in your web applications with precision and confidence.