6

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:

  1. How to format numbers by prepending 0 to single-digit numbers?
  2. How to output numbers with leading zeros in JavaScript [duplicate]
  3. 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.

Tanzeel
  • 4,174
  • 13
  • 57
  • 110
  • 1
    as far as i know most languages do not allow a number to start with 0 because either ways the arithmetic result will be the same – ellipsis Feb 10 '20 at 06:37
  • 2
    You can't do it without changing the data type. `'01' ... '09'` are strings, if you parseInt('01') then you'll have a number, `console.log(01)` will output as `1`. You can't write a zero before single digits. – darklightcode Feb 10 '20 at 06:38
  • 2
    Numbers have no format, only a value. Doesn't matter wether you write `12`, `0xC`, `0.12e2` or `0b1100`. 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. – Thomas Feb 10 '20 at 06:38
  • I'm afraid that I'm asking the wrong question and wasting my and everyone's time and efforts. I'm really sorry if i did. – Tanzeel Feb 10 '20 at 06:38
  • 1
    @Tanzeel you can do your calculations with or without a leading 0 the answer will never change. For representation you can do it using strings and all the answers show one way or another to do it – ellipsis Feb 10 '20 at 06:43
  • 1
    Sidenote @ellipsis: JS allows you to write numbers with leading zeroes. But they are a real issue, because **sometimes** they will get interpreted as octal and **sometimes** as decimal; and Tanzeel they still don't keep that leading zero because it's just another way to write a value. – Thomas Feb 10 '20 at 06:44
  • @ellipsis, thomas.I've learnt something new today. thanks a lot. :-) – Tanzeel Feb 10 '20 at 06:58

3 Answers3

12

You cannot add a 0 in front of a number, without also turning it into a string.

To do both:

(5).toString().padStart(2,0);  // returns "05"
Evert
  • 93,428
  • 18
  • 118
  • 189
1

Try something like this:

function convert(n) {
  n = String(n)
  if (n.length == 1)
    n = '0' + n
  return n
}

convert(0) //  -> "00"
convert(1) //  -> "01"
convert(9) //  -> "09"
convert(14) // -> "14"
convert(20) // -> "20"
F.NiX
  • 1,457
  • 3
  • 12
  • 20
0

I haven't used TypeScript much but since it is strongly typed, and you have initially declared monthStartingIndex as a number, I assume that's causing the error. Can you try the following?

Also, the snippet does not really make sense since you're converting to string and then using it for iteration. Maybe you want to do that inside the for loop?

monthSlice() {
    let monthStartingIndexValidate = "0";
    monthStartingIndexValidate = ("0" + monthStartingIndexValidate).slice(-2); //error
    let monthEndingIndex = 24;
    for(monthStartingIndex = 0; monthStartingIndex < monthEndingIndex; monthStartingIndex++) {
    ...
    } 
}
Gaurav Punjabi
  • 153
  • 1
  • 6
  • No Gaurav, I cant change the data type of my variables. there's a lot of arithmetic waiting to be implemented very soon. – Tanzeel Feb 10 '20 at 06:21
  • @Tanzeel Then you need to do this in another variable using this value. I'm not sure what you want to do with this value, could you explain? – Gaurav Punjabi Feb 10 '20 at 06:25
  • I'm doing this because I've to validate month. and the format is `MM-DD-YYYY`. That's why I want them to be `01, 02, 03, ...09` to comply with `MM`. Otherwise validation is failing. And I can't alter the format `MM-DD-YYYY`. My tech lead will not allow. :-( – Tanzeel Feb 10 '20 at 06:28
  • Can you assign the padded value to a new variable with type string and then validate it? – Gaurav Punjabi Feb 10 '20 at 06:28
  • Sorry Sir. I didn't understand this. Can you please explain a little bit more. :-) – Tanzeel Feb 10 '20 at 06:31
  • @Tanzeel Check edit. Use a different variable for validation and a different variable for arithmetic. – Gaurav Punjabi Feb 10 '20 at 06:33