-1

I have the following object

{
"date":"2019-07-22 16:16:45.000000",
"timezone_type":3,
"timezone":"America/Sao_Paulo"
}

i need to convert it to a JavaScript Date. If i try JSON.parse it says "invalid date".

I'd rather not have turned it into a json at all, but it is part of a bigger object that i need serialized whole.

Bruno Souza
  • 198
  • 1
  • 12

2 Answers2

1

You can simply access it as you know it:

const o = {
  "date":"2019-07-22 16:16:45.000000",
  "timezone_type":3,
  "timezone":"America/Sao_Paulo"
}
const date = new Date(o.date)
console.log(date)
const now = new Date()
console.log(now)

https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Date

note: of course you can also read those other values and pass it to the Date object

messerbill
  • 5,499
  • 1
  • 27
  • 38
0

This could be a possible solution:

var str = "2019-07-22 16:16:45.000000"
var date = new Date(str);
Dino
  • 7,779
  • 12
  • 46
  • 85
Nathan Kolpa
  • 89
  • 1
  • 11