-1

I have an API that accepts a date in this format 20.02.2020, but I have to save it in MySql, so I need this format 2020/02/20, I have tried using replace function and regex. But didn't get the desired output.

let dt='20.02.2020';
dt.replace('/./g','')

2 Answers2

1

Import moment through npm and pass desired dataFormat as argument.

  moment(date, moment.ISO_8601, true).local().format(dateFormat)
0

Here's one of many regex solutions:

dt.replace(/([0-9]*?)\.([0-9]*?)\.([0-9]*)/gmi, '$3\/$2\/$1');

I find the most elegant solution if you have to reuse dt's value is the split reverse and join which was demonstrated by Tewathia

Beavatron Prime
  • 309
  • 2
  • 4