Unix timestamp from date

JavaScript, Date · Oct 19, 2020

Gets the Unix timestamp from a Date object.

  • Use Date.prototype.getTime() to get the timestamp in milliseconds and divide by 1000 to get the timestamp in seconds.
  • Use Math.floor() to appropriately round the resulting timestamp to an integer.
  • Omit the argument, date, to use the current date.
const getTimestamp = (date = new Date()) => Math.floor(date.getTime() / 1000);
getTimestamp(); // 1602162242

More like this

  • Date from Unix timestamp

    Creates a Date object from a Unix timestamp.

    JavaScript, Date · Oct 15, 2020

  • Get colon time from date

    Returns a string of the form HH:MM:SS from a Date object.

    JavaScript, Date · Oct 19, 2020

  • Day of year

    Gets the day of the year (number in the range 1-366) from a Date object.

    JavaScript, Date · Oct 19, 2020