How to Convert Date to Timestamp in JavaScript

Javascript date and time are essential objects that store date and time information. For example, in taking the subscription of any particular web application, behind the scenes, the Javascript will take the timestamp to keep the information related to the date and time of the subscription.

Javascript has built-in date and time objects. However, using the timestamp in most practical applications is more convenient than a particular date format like hours, minutes, seconds, etc. The timestamp gives us more detailed information about the time and event when the action took place. It provides complete and accurate information about the time.

Unix timestamp in JavaScript

To get the Unix timestamp in Javascript, use the getTime() function. It is a built-in Date and Time object that returns the number of milliseconds. So the user should divide the obtained value by 1000 to get the timestamp value in seconds.

Example

Math.round(new Date().getTime()/1000);

Output

1647408959

Explanation

  1. The new Date() constructor is used to initialize any value of a variable. The getTime() is a built-in JavaScript method to get the time in milliseconds.
  2. Since one second is equivalent to 1000 milliseconds it must be divided by 1000 to get the value in terms of seconds.
  3. Javascript Math.round() is a built-in function used to round off the value in second to the nearest integer value. This is however optional unless you need the timestamp in integer format.

    JavaScript date to timestamp

    To convert date to a timestamp in JavaScript, Date.UTC() function. The UTC() function takes three required positional parameters and four optional parameters. The Year, Month, and Day are the three required parameters in the function, and Hours, minutes, seconds, and milliseconds are the four optional arguments in the function.

    Syntax

    Date.UTC(year)
    Date.UTC(year, month)
    Date.UTC(year, month, day)
    Date.UTC(year, month, day, hour)
    Date.UTC(year, month, day, hour, minute)
    Date.UTC(year, month, day, hour, minute, second)
    Date.UTC(year, month, day, hour, minute, second, millisecond)

    Example

    let datum = new Date(Date.UTC('2009','01','13','23','31','30'));
    console.log(datum.getTime()/1000);
    

    Output

    1234567890
    1. The new Date() constructor is used to initialize a value in the form of date in Javascript. The ‘2009’,’01’,’13’,’23’,’31’,’30’ is a series of string representing year, month, day, hour, minute, second respectively.
    2. While the first three parameters ‘2009’, ’01’,’13’ are compulsory, the parameters after they are not compulsory.
    3. Again the value of the variable where the date is stored must be divided by 1000 to get the answer in seconds. If not divided by 1000, you will get the timestamp in milliseconds instead.

    JavaScript Date.parse()

    The Date.parse() is a built-in JavaScript function that returns the number of milliseconds since January 1, 1970, 00:00:00 UTC. The date is obtained in string data types by default in Javascript. This can be converted into a timestamp using the Date.parse() method.

    If the string date string is unrecognized, then it will return NaN. But, this is not a recommended method to convert data to the timestamp. For example, January 1, 1970, is entirely taken as a reference time, and there is no big /valid reason to choose this date as the reference time.

    Syntax

    Date.parse(date String)

    Parameters

    The date.parse() function takes a date argument, a string representation of a simplification of the ISO 8601.

    Return value

    The date.parse() method returns a number representing the milliseconds that have elapsed since January 1, 1970, 00:00:00 UTC.

    Example

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

    Output

    1643824890

    Explanation

    The toTimeStamp() is a user-defined function. So the user is free to use any name for the function. The strDate() is a user-defined variable, so the user is free to choose the name of this variable too.

    The Date.parse() is a necessary built-in function here and cannot be altered with another name.

    We got 1643824890 in the output means that since January 1, 1970, 00:00:00 UTC 1643824890 milliseconds had passed when the user got the timestamp.

    Invalid format

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

    Output

    NaN

    If you pass an invalid format to the Date.parse() method, it returns NaN.

    That’s it for this tutorial.

    Leave a Comment