2

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 ??

John Slegers
  • 38,420
  • 17
  • 182
  • 152
Haytham
  • 744
  • 1
  • 9
  • 24

2 Answers2

1

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)/"));
John Slegers
  • 38,420
  • 17
  • 182
  • 152
0

It is unclear from your question.

Providing the time you have in the initial question is EPOCH time (seconds passed from 1970) you can use .toISOstring and that should get you close enough to what you want.

https://www.w3schools.com/jsref/jsref_toisostring.asp

  • You haven't shown the OP how to parse the string to a Date. *toISOString* returns a UTC date and time and is not in the format required. – RobG May 10 '18 at 21:34