How to Convert Date to Timestamp in JavaScript [4 Ways]

    Here are the four ways to convert Date to Timestamp:

    1. Using Date.parse()
    2. Using Date.prototype.getTime()
    3. Using Moment’s unix()
    4. Using valueOf()

    Method 1: Using the Date.parse() method

    JavaScript Date.parse() method returns the number of milliseconds since January 1, 1970, 00:00:00 UTC.

    const toTimeStamp = (strDate) => { 
      const dt = Date.parse(strDate); 
      return dt / 1000; 
    console.log(toTimeStamp('02/02/2022 23:31:30'));



    Method 2: Using the Date.prototype.getTime() method

    You can also use the Date.prototype.getTime() method. It returns the number of milliseconds since the Unix epoch (January 1, 1970 00:00:00 UTC), but Unix timestamps are typically represented in seconds.

    To get the Unix timestamp in seconds, divide the result by 1000.

    // Create a Date object for the current date and time
    const date = new Date();
    // Convert the Date object to a Unix timestamp (in seconds)
    const timestamp = Math.floor(date.getTime() / 1000);



    Method 3: Moment.js’s unix() Method

    We can use the “Moment.js’s unix()” method to return a timestamp.

    const moment = require("moment")
    const toTimestamp = (strDate) => {
      const dt = moment(strDate).unix();
      return dt;
    console.log(toTimestamp("02/13/2023 23:31:30"));


    Deprecation warning: value provided is not in a recognized RFC2822 or ISO format. 
    moment construction falls back to js Date(), 
    which is unreliable across all browsers and versions. 
    Non RFC2822/ISO date formats are discouraged. 
    Please refer to for more info.

    Method 4: Using the valueOf() function

    JavaScript valueOf() method returns the primitive value of the specified object. It can be used with Numbers, Strings, Boolean, and Dates. When called on an object, the valueOf() method returns the primitive value representing the object.

    const date = new Date();
    const timestamp = Math.floor(date.valueOf() / 1000);



    That’s all!