I want to convert 1,2,3,..9,10,11,12...24 to 01,02,03,...09,10,11,12,...24. I tried these solutions. None of these solved my purpose:
- How to format numbers by prepending 0 to single-digit numbers?
- How to output numbers with leading zeros in JavaScript [duplicate]
- How can I pad a value with leading zeros?
I'm working on an Angular project and I'm doing this in Typescript. Here is my code:
monthpikcer.component.ts
monthSlice() {
let monthStartingIndex = 0;
monthStartingIndex = ("0" + monthStartingIndex).slice(-2); //error
let monthEndingIndex = 24;
for(monthStartingIndex =0; monthStartingIndex < monthEndingIndex; monthStartingIndex++) {
...
}
}
But for line monthStartingIndex = ("0" + monthStartingIndex).slice(-2), I'm getting an error:
Type 'string' is not assignable to type 'number'.
I want it to be 01, 02, 03...10,11,12,13...24. Please tell me how to do it without changing the data type of my variables because later I've to do some arithematics with them.
'01' ... '09'are strings, if you parseInt('01') then you'll have a number,console.log(01)will output as1. You can't write a zero before single digits.12,0xC,0.12e2or0b1100. All the same value. So work with the numbers! format them only when you render them. I don't know which Angular version you use, and I don't have experience with Angular.io, but in angularjs you could write a filter to do a job like this.