Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Returning JavaScript as ddmmyyyy with zeros

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Much appreciated!

>Solution :

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());
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading