robertkrimen / otto

A JavaScript interpreter in Go (golang)
http://godoc.org/github.com/robertkrimen/otto
MIT License
8.01k stars 584 forks source link

new Date can't not parse datetime string #489

Closed lyr-2000 closed 1 year ago

lyr-2000 commented 1 year ago

new Date can't not parse time string format

var t = '2023/02/23 11:23:57'
console.log( new Date (t)) // it 's wrong , get     Invalid Date
console.log( new Date( 1677122637000)  )
console.log( new Date( 1677122637000).toLocaleString()  )

//output:

Invalid Date
Thu, 23 Feb 2023 11:23:57 CST
2023-02-23 11:23:57

expect output is

Thu Feb 23 2023 11:23:57 GMT+0800
Thu Feb 23 2023 11:23:57 GMT+0800
2023-02-23 11:23:57
stevenh commented 1 year ago

While browsers may support this it isn't a mandated part of the spec:

Only the ISO 8601 format (YYYY-MM-DDTHH:mm:ss.sssZ) is explicitly specified to be supported. Other formats are implementation-defined and may not work across all browsers. A library can help if many different formats are to be accommodated.

You can see the formats that otto currently supports here

That said it's easy to add please try https://github.com/robertkrimen/otto/pull/490 to confirm it works for you.

stevenh commented 1 year ago

@lyr-2000 did you manage to test #490 ?