0

How can i format a date in javascript?

let date = '2022-01-01';
let fd = date.toLocaleString("nl-NL"); // in dutch format
alert(fd); // outputs also 2022-01-01; should be 01-01-2022
CertainPerformance
  • 313,535
  • 40
  • 245
  • 254
Jack Maessen
  • 1,708
  • 4
  • 17
  • 42

2 Answers2

0

You are not constructing a Date() object.

const date = new Date('2022-01-01')
let fd = date.toLocaleString("nl-NL");
//or make it by yourself
//let fd = date.getDate() + '-' + (date.getMonth() + 1) + '-' + date.getFullYear()
console.log(fd); 
Vaggelis
  • 593
  • 2
  • 13
  • See [*Why does Date.parse give incorrect results?*](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results) – RobG Jan 09 '22 at 20:13
-1

let date = '2022-01-01';
var options = {year: "numeric", month: "numeric", day: "numeric"};
fd = (new Intl.DateTimeFormat("en-AU", options).format(new Date(date))).replace(/\//g, '-');
alert(fd); 

try this.

Hasibul Hasan
  • 45
  • 1
  • 3
  • See [*Why does Date.parse give incorrect results?*](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results) – RobG Jan 09 '22 at 20:14