0

How to parse json date like /Date(1391802454790-0700)/ to (01/31/2014 11:44 AM) in NodeJS.

JSON Date Object:

{
    ...,
    "BirthDate":"\/Date(1391802454790-0700)\/",
    ...   
}

I tried below code but it doesn't work:

var jsondate = "/Date(1391802454790-0700)/";
const data = JSON.parse(jsondate);
console.log(data);
gotorres.gt18
  • 13
  • 1
  • 5
  • 1
    Does this answer your question? [How do I format a Microsoft JSON date?](https://stackoverflow.com/questions/206384/how-do-i-format-a-microsoft-json-date) – tmarwen Aug 16 '21 at 15:26

2 Answers2

0

uhmm, You just gotta use the value of Birthdate and replace the unwanted characters so you can obtain 1391802454790 part; after that you just need to create a valid date out of this 1391802454790 in the desired format with new Date():

 let data = {
        ...,
        "BirthDate":"\/Date(1391802454790-0700)\/",
        ...   
    };
    let birthdate = data.Birthdate;
    birthdate = birthdate.replace("/Date(", "").replace("-0700)/", "");
    let formatedBirthdate = new Date(birthdate);
BlackSheep
  • 573
  • 2
  • 7
  • 19
  • it works too, I just only add this line: ** let milliseconds = Number(birthdate); ** before this: ** let formatedBirthdate = new Date(milliseconds); ** – gotorres.gt18 Aug 16 '21 at 16:33
0
let obj = {
  birthday: "/Date(1391802454790-0700)/",
};

let regex = /\(([^)]+)\-\d+/g;

let match = regex.exec(obj.birthday); 
if (match) {
  console.log(match[1]);
  console.log(new Date(Number(match[1])));  // 2014-02-07T19:47:34.790Z - You can use libs to format date
}