-1

I intend to convert the below value in ISO8601 to the corresponding string with a format like DD-MM-YYYY HH:MI:SSXFF TZR

Below is the ISO8601 string value,

2022-05-05T18:08:13.311Z

To the below format,

2022-05-05 18:08:13.311951 +0:00

Are there any libraries or a way to achieve in JavaScript compatible with Rhino v13?

Heretic Monkey
  • 11,078
  • 7
  • 55
  • 112
Karu
  • 49
  • 6
  • An ISO8601 string value can be passed to the Date constructor, then you can use the duplicate's answers to format it however you please. – Heretic Monkey May 09 '22 at 14:40
  • @HereticMonkey I think this ISO format cannot be parsed by date constructor. It gives me an invalid date as a response when I do var dt = new Date ('2022-05-05T18:08:13.311Z') – Karu May 09 '22 at 14:48
  • Works on Edge Version 101.0.1210.32 (Official build) (64-bit) on Windows 10. – Heretic Monkey May 09 '22 at 14:50
  • @HereticMonkey This is where I am limited to some of the functions. Rhino JS engine version 13 does not support this. – Karu May 09 '22 at 14:51
  • 2
    Where are you expecting the additional information to come from? "2022-05-05 18:08:13.311951 +0:00" has three more decimals of precision than "2022-05-05T18:08:13.311Z". – Heretic Monkey May 09 '22 at 14:54
  • As far as working with Rhino goes, look at the answers to [Parsing a string to a date in JavaScript](https://stackoverflow.com/q/5619202/215552) – Heretic Monkey May 09 '22 at 14:56
  • 1
    @Karu to further Heretic Monkey's point, even if you can't feed it directly to the Date constructor in the Rhino implementation you can extract the number values with string splits and feed them piecemeal, or just use string functions to manipulate. There are about a jillion examples of this already on the site. – Jared Smith May 09 '22 at 14:57
  • @Karu Not sure why you say the ISO format cannot be parsed using the date constructor in Rhino. Just tried both `new Date ('2022-05-05T18:08:13.311Z')` and `new Date(Date.parse('2022-05-05T18:08:13.311Z'))` and both result in the correct date, outputting `Thu May 05 2022 20:08:13 GMT+0200 (CEST)` when in the CEST timezone ofcourse – Paul May 10 '22 at 17:47
  • @Paul Right. I was making a mistake here. If you try to parse '2022-05-05T18:08:13.311951Z. It will return an invalid date. I think precision till 3 digits for milliseconds is supported in rhino js. – Karu May 11 '22 at 05:37
  • @Karu, ah, yeah, then what you're observing in Rhino is correct: According to the EcmaScript specification implementations MUST support at least the ISO format with a 3 digit value for the milliseconds. Implementations are allowed to support additional formats, but those are up to the specific implementation and thus aren't mandatory. Rhino indeed doesn't support millisecond values with more than 3 digits in the ISO format – Paul May 11 '22 at 06:13
  • filed an issue with Rhino to allow parsing ISO date strings with millisecond precision > 3 digits: https://github.com/mozilla/rhino/issues/1223 – Paul May 11 '22 at 06:32

0 Answers0