2

I saw a lot of answers for the date problem with Safari and IE for the date, using replace(/-/g, "/") works like a charm for these cases 2022-11-30 17:00 UTC+0200 but encountered an issue when I had other time zone like this one 2022-11-28 21:56 UTC-0500 it would create an invalid date again, for any browser.

So I'm looking for a solution that would replace the "-" not globally but only in the first word eventually.

Thank you

Yohav Rn
  • 159
  • 1
  • 11
  • You would save yourself a lot of trouble by using iso format – Konrad Nov 07 '22 at 21:04
  • @KonradLinkowski They've encountered [this annoying problem](https://stackoverflow.com/questions/4310953/invalid-date-in-safari). – kelsny Nov 07 '22 at 21:05
  • @caTS is it also broken for `yyyy-mm-ddThh:mm:ss`? – Konrad Nov 07 '22 at 21:10
  • @KonradLinkowski I don't think OP can change the format of the input - otherwise, why would they be asking about this? – kelsny Nov 07 '22 at 21:11
  • I can't indeed, that's how I receive the datas – Yohav Rn Nov 07 '22 at 21:12
  • If the purpose of reformatting is to hand it to the built–in parser, then you should format it exactly to a supported format (there are three), not another implementation dependent format. – RobG Nov 08 '22 at 05:37

2 Answers2

2

What about that?

const date = `2022-11-30 17:00 UTC-0200`
const regex = /(\d+)-(\d+)-(\d+)/g
const result = date.replace(regex, '$1/$2/$3')
console.log(result)
kelsny
  • 23,009
  • 3
  • 19
  • 48
Konrad
  • 21,590
  • 4
  • 28
  • 64
  • My problem is not so much with those dates 2022-11-30 17:00 UTC+0200, but more with the negative UTC, that is also transformed into a / and therefore make the date not valid 2022-11-28 21:56 UTC-0500 – Yohav Rn Nov 07 '22 at 21:11
  • 2
    @YohavRn It does work with "negative UTC", see the tiny edit. – kelsny Nov 07 '22 at 21:12
1

You can optionally capture UTC before - and if it is captured, put back the whole match, else, replace - with /:

const text = "2022-11-28 21:56 UTC-0500";
console.log( text.replace(/(UTC)?-/g, (x,y) => y ? x : "/") )

Here,

  • (UTC)?- - matches and captures an optional UTC in Group 1 and then matches a - char
  • (x,y) => y ? x : "/" - if y (Group 1) was matched, put back x (the whole match), else, put / instead of -.

A lookbehind version:

const text = "2022-11-28 21:56 UTC-0500";
console.log( text.replace(/(?<!UTC)-/g, "/") )
// => 2022/11/28 21:56 UTC-0500

(?<!UTC)- matches a - that is not immediately preceded with UTC.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • The second example throws a script error in Safari at least: "Invalid regular expression: invalid group specifier name". – RobG Nov 08 '22 at 05:38
  • I know, it is meant for the contemporary browsers that support ECMAScript 2018+. – Wiktor Stribiżew Nov 08 '22 at 08:06
  • It fails in Firefox on iPad too. – RobG Nov 08 '22 at 14:33
  • @RobG It does not matter, it is only for those that support ECMAScript 2018+ as I said. I specifically mention it is only "a lookbehind version". For those that want to learn regex. And those who use compliant engines. And that is why the first option is at the top of the answer, as it works with ES5. – Wiktor Stribiżew Nov 08 '22 at 14:46
  • Given the OP asks about Safari then it’s worth mentioning in the answer that the look behind version doesn’t work in Safari (and others). – RobG Nov 08 '22 at 20:51
  • @RobG Now I see your point, why didn't you mention it in the top comment? – Wiktor Stribiżew Nov 08 '22 at 20:53