You can generate a timestamp in JavaScript by using the Date object and its .getTime() method. The .getTime() method returns the number of milliseconds since January 1, 1970, 00:00:00 UTC.


let timestamp = new Date().getTime();

You can also generate a timestamp in a more human-readable format using the toString() method.

let timestamp = new Date().toString();

You can also create a string with a specific format using the toLocaleString() method.

let timestamp = new Date().toLocaleString();

This will return a string in the format of: “Month Day, Year Hour:Minutes:Seconds”
You can also pass the options to format the date and time as per the requirement.

let timestamp = new Date().toLocaleString('en-US', {
  month: 'short',
  day: '2-digit',
  year: 'numeric',
  hour: '2-digit',
  minute: '2-digit',
  second: '2-digit'

This will return the timestamp in format : ‘MM/DD/YY HH:mm:ss’

You can also use libraries like moment.js which provides a more powerful way to handle dates and timestamps.

Also Read:

Categorized in: