-2

I have this value:

this.value.day

It returns a number from 1 to 31.

However, I'd like to insert a 0 if it's less than 10, how can I do that?

Rosenberg
  • 2,424
  • 5
  • 33
  • 56
  • 4
    You can use `padStart` from the String prototype: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padStart – Hunter McMillen Aug 16 '18 at 21:39
  • 2
    Possible duplicate of [How can I pad a value with leading zeros?](https://stackoverflow.com/questions/1267283/how-can-i-pad-a-value-with-leading-zeros) – ConnorsFan Aug 16 '18 at 22:35

2 Answers2

2

One liner and cross browser compatible

newValue = ('0' + this.value.day.toString()).slice(-2);
wFitz
  • 1,266
  • 8
  • 13
0

Well, this is not an elegant solution, but it gets the job done.

if (this.value.day < 10) {
  this.dayRender = "0" + this.value.day;
} else {
  this.dayRender = this.value.day;
}
Rosenberg
  • 2,424
  • 5
  • 33
  • 56