-1

I am still learning JS, but I am stuck on a little challenge where I need to output date to ddmmyyyy. Don't know really how do I add that or it's just a brain error.

This is as far as I have come:

var d= new Date(); var m=((d.getMonth()+1)<10)?'0'+(d.getMonth()+1):(d.getMonth()+1); return d.getDate() + m + d.getFullYear();

I tried adding .padStart(2, "0"); to the day, but it won't work and told that it's not a function.

Much appreciated!

nerdface
  • 1
  • 1
  • You need to turn the number into a string first to be able to use .padStart() - like `String(someNumber).padStart(2, "0")` –  Apr 01 '22 at 06:19

1 Answers1

0

Try this

var d= new Date();
var m=((d.getMonth()+1)<10)?'0'+(d.getMonth()+1):(d.getMonth()+1);
// See the changes in the line below    
console.log(d.getDate().toString().padStart(2, '0') + m + d.getFullYear());
Suraj Sanwal
  • 728
  • 1
  • 6
  • 14