0

I encountered an unexpected issue with "${calculate.createdAt.getMinutes()}" It does not show the 0... It should be "13:06" but it shows only 13:6 . How to fix the missing 0? Thanks for advice.

  • 1
    `getMinutes` returns a number, and when you use a number in a template string, it's not padded. If you want it to be padded, you have to do it yourself, for example with [`padStart`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padStart) – derpirscher Jun 17 '21 at 11:21

1 Answers1

1

You can apply padStart like this

let date = new Date(2021,6,10,10,3,20)
console.log(date);
let minuteString = `${date.getMinutes()}`.padStart(2, "0")
console.log(minuteString);
derpirscher
  • 14,418
  • 3
  • 18
  • 35
  • Nope... shows parsing error due to the back ticks. – Stanislav Brusnicky Jun 17 '21 at 13:44
  • I don't know, what you did with that piece of code, but using backticks is the way to go for string tempates ... – derpirscher Jun 17 '21 at 13:51
  • @Stanislav Perhaps your browser or build setup doesn't support template strings. Alternatively, you could convert the number to string beforehand: `date.getMinutes().toString().padStart(2, '0')` – tony19 Jun 18 '21 at 03:42