0

Hi All this is mostly asked question still i didn't find easy and direct way to convert this in to date format.

current format is in

"/Date(1535515200000)/"

if i executed the below line that will give the needed date format. Is there any direct way to get the date format from "/Date(1535515200000)/" to Wed Aug 29 2018 00:00:00 GMT-0400 (Eastern Daylight Time)

new Date(1535515200000)
Webber
  • 184
  • 2
  • 13
  • That looks like a "timestamp"; check this out: https://stackoverflow.com/questions/847185/convert-a-unix-timestamp-to-time-in-javascript – Chris Cousins Aug 27 '18 at 19:42
  • date am getting from a API is in string format of "/Date(1535515200000)/" i need to convert to Wed Aug 29 2018 00:00:00 GMT-0400 (Eastern Daylight Time) – Webber Aug 27 '18 at 19:46
  • I pretty much always use this package when working with javascript datetimes -> https://momentjs.com/ it handles the conversion for that format with no issues and many many many more. – Paul Swetz Aug 27 '18 at 19:59

2 Answers2

0
var s = "/Date(1535515200000)/";
var ts = s.substring(s.indexOf('(')+1,s.lastIndexOf(')'));
console.log(new Date(Number(ts)).toISOString());

Would print "2018-08-29T04:00:00.000Z"

Philipp
  • 4,180
  • 7
  • 27
  • 41
0

If you are doing it from JSON.parse, you can use the reviver parameter and parse it out.

const x = JSON.parse('{"foo": "/Date(1535515200000)/"}', (key, value) =>
  typeof value === 'string' && value.startsWith("/Date(")
    ? new Date(+value.match(/(\d+)/)[0]) // return new date
    : value     // return everything else unchanged
);
console.log(x);
epascarello
  • 204,599
  • 20
  • 195
  • 236