0

In the new Date () method, minutes up to 10 are displayed without 0 (for example, 17:05 is displayed as 17: 5; 18:09 as 18: 9, ...). Why is 0 not displayed in minutes?

new Date().getHours() +":"+ new Date().getMinutes() +"; "+new Date().getDate() + " "+ (new Date().toLocaleString("en-us", { month: "long" })) + " "+new Date().getFullYear()

1 Answers1

-1

Because getMinutes returns a number, not a (formatted) string. You have to format the number yourself as string. For example:

const lead0 = n => String(n).padStart(2, "0");

console.log(lead0(10)) // "10"
console.log(lead0(1)) // "01"
ZER0
  • 24,846
  • 5
  • 51
  • 54
  • 1. The name of the function is totally irrelevant, and is – as your "imvho" also suggest – a matter of personal taste. 2. That's why I wrote a generic function that add a "leading zero" to a number, instead of getting as argument a `Date` object. 3. feel free to point the OP to the answers of the duplicates you're mention. – ZER0 Mar 24 '20 at 18:06
  • You mean like in [this dupe](https://stackoverflow.com/questions/1267283/how-can-i-pad-a-value-with-leading-zeros), or [this](https://stackoverflow.com/questions/6040515/how-do-i-get-month-and-date-of-javascript-in-2-digit-format), or [this](https://stackoverflow.com/questions/3605214/javascript-add-leading-zeroes-to-date), or ... – Andreas Mar 24 '20 at 18:11
  • Yep. But I didn't see you help the OP in such way, so I helped as I could. As said, feel free to contribute as you want to, and I will do the same. :) – ZER0 Mar 24 '20 at 18:12