Search code examples
javascriptdatetimestampdate-formattime-format

Convert timestamp to a specific date format in javascript


I am trying to convert a timestamp of this format in Javascript

/Date(1231110000000)/

to this format:

DD/MM/YYYY

Does anyone know how to do it ??


Solution

  • How you can convert /Date(1231110000000)/ to DD/MM/YYYY format :

    function convert(timestamp) {
      var date = new Date(                          // Convert to date
        parseInt(                                   // Convert to integer
          timestamp.split("(")[1]                   // Take only the part right of the "("
        )
      );
      return [
        ("0" + date.getDate()).slice(-2),           // Get day and pad it with zeroes
        ("0" + (date.getMonth()+1)).slice(-2),      // Get month and pad it with zeroes
        date.getFullYear()                          // Get full year
      ].join('/');                                  // Glue the pieces together
    }
    
    
    console.log(convert("/Date(1231110000000)/"));