0

How to get date time from this /Date(1518696376959)/ in javascript? I have tried like this

var d = new Date("1519192874994");

tryingToLearn
  • 10,691
  • 12
  • 80
  • 114
Akram Khan
  • 114
  • 8
  • 1
    what day does that number suppose to be? – Tree Nguyen Feb 21 '18 at 06:21
  • `let d = new Date(+"/Date(1518696376959)/".match(/\d+/)[0]);` Note that this has no error handling but just poses as an example for getting the number out of the string and converting to number. – ASDFGerte Feb 21 '18 at 06:21
  • 3
    Possible duplicate of [How do I format a Microsoft JSON date?](https://stackoverflow.com/questions/206384/how-do-i-format-a-microsoft-json-date) – Alexei Levenkov Feb 21 '18 at 06:22
  • Possible duplicate of [Convert a Unix timestamp to time in JavaScript](https://stackoverflow.com/questions/847185/convert-a-unix-timestamp-to-time-in-javascript) – Fortes Feb 21 '18 at 06:25
  • Alexei thanks its working – Akram Khan Feb 21 '18 at 06:39

1 Answers1

1

I have tried like this var d = new Date("1519192874994");

No need to wrap it in quotes, Date constructor will takes millisecond value (Number)

var d = new Date(1519192874994) //Wed Feb 21 2018 11:31:14 GMT+0530 (India Standard Time)

From "/Date(1518696376959)/"

Make it

var str = "/Date(1518696376959)/";
var date = new Date( +str.match(/\d+/g)[0]  ); //notice the unary plus after getting the match
gurvinder372
  • 66,980
  • 10
  • 72
  • 94