Skip to main content

How to Convert a Date to a Timestamp in JavaScript

A Unix timestamp is the number of milliseconds that have elapsed since the Unix Epoch (January 1, 1970, 00:00:00 UTC). This numeric representation is incredibly useful for storing dates, performing date arithmetic, and for consistent data exchange between systems.

This guide will teach you the modern and standard methods for converting a Date object or a date string into a timestamp. You will learn the difference between getting a timestamp in milliseconds versus seconds and the best practices for parsing date strings.

The Core Method: Date.prototype.getTime()

The most fundamental way to get a timestamp from an existing Date object is to use the getTime() method.

Problem: we have a Date object and need its numeric timestamp representation.

// Problem: How to get the timestamp for this date?
const myDate = new Date('2023-10-27T10:00:00Z');

Solution: the getTime() method returns the number of milliseconds since the Unix Epoch.

const myDate = new Date('2023-10-27T10:00:00Z');

const timestamp = myDate.getTime();

console.log(timestamp); // Output: 1698400800000
note

This is the standard, instance-based method for getting a timestamp.

The Modern Shortcut: Date.now()

If you only need the timestamp for the current moment, you do not need to create a Date object first. The static Date.now() method provides a direct and more performant way to get the current timestamp.

// Gets the current timestamp in milliseconds without creating a Date object.
const currentTimestamp = Date.now();

console.log(currentTimestamp); // Output: (e.g.) 1760093998040

This is the recommended best practice for getting the current timestamp.

Converting a Date String to a Timestamp

To get a timestamp from a date string, you must first parse it into a Date object. The Date.parse() static method is perfect for this, as it directly returns a timestamp.

note

Important: The Date.parse() method works most reliably with ISO 8601 formatted strings (YYYY-MM-DDTHH:mm:ss.sssZ). Other formats can be ambiguous and lead to bugs.

Problem: we have a date string and need to convert it directly to a timestamp.

// Problem: How to convert this string to a timestamp?
const dateString = '2023-10-27';

Solution: Date.parse() will read the string and return its timestamp in milliseconds.

const dateString = '2023-10-27';
const timestamp = Date.parse(dateString);

console.log(timestamp); // Output: 1698364800000 (Varies by timezone)

If you pass a string in a non-standard format (like MM/DD/YYYY), parsing can be unreliable across different browsers and locales. In such cases, you should manually parse the string and construct the Date object yourself.

// For non-standard formats, parse manually.
const dateStr = '10/27/2023';
const [month, day, year] = dateStr.split('/');
const myDate = new Date(year, month - 1, day); // month is 0-indexed
const myTimestamp = myDate.getTime();

Converting Timestamps from Milliseconds to Seconds

By convention, Unix timestamps are often represented in seconds, not milliseconds. To convert from JavaScript's millisecond-based timestamp to a standard seconds-based timestamp, simply divide by 1000 and remove the decimal part.

const timestampInMs = Date.now();

const timestampInSeconds = Math.floor(timestampInMs / 1000);

console.log('Milliseconds:', timestampInMs); // Output: 1760094032754
console.log('Seconds:', timestampInSeconds); // Output: 1760094032
note

Math.floor() is used to ensure you get a whole number.

Conclusion

Converting dates to timestamps is a straightforward process in JavaScript if you use the right tools.

  • To get a timestamp from an existing Date object, use the myDate.getTime() method.
  • To get the current timestamp, use the more direct and performant Date.now().
  • To convert a date string to a timestamp, use Date.parse(), making sure your string is in the reliable ISO 8601 format (YYYY-MM-DD).
  • To convert a millisecond timestamp to seconds, divide by 1000 and use Math.floor().

By following these modern best practices, you can handle date-to-timestamp conversions reliably and efficiently.