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
  • 356,069
  • 52
  • 309
  • 320
Jack Maessen
  • 1,780
  • 4
  • 19
  • 51

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
  • 912
  • 5
  • 17
  • 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
  • 53
  • 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