-1

How would i use javascript or jquery to add a leading zero to this?

for (im=1;im<=31;im++){
     days[im]=everyDay[im];
}
4
  • Do you want days[im] to always be a string of length 2? What is everyDay? Apr 23, 2012 at 3:41
  • @MikeSamuel yea it would only be for the days of the month
    – Craig
    Apr 23, 2012 at 3:43
  • 7
    everyDay[im].shufflin'
    – DanRedux
    Apr 23, 2012 at 3:47
  • @Craig everyDay array consist what? Is it consists both string and integers
    – Valli69
    Apr 23, 2012 at 7:27

5 Answers 5

1

Consider:

for (var t, im=1; im<=31; im++){ 
  t = everyDay[im]; 
  days[im] = (t < 10? 0 : '') + t; 
}
0
0

Prepend it with a 0, and then take the last two characters:

var days = {};
for (im=1;im<=31;im++){
     days[im] = ('0' + im).substr(-2);
}
2
  • This gives me the zeros up to 09 but also 10 becomes 01. I'm guessing becuase it adds a zero then subtracts the 3 number 20 becomes 020 then 02
    – Craig
    Apr 23, 2012 at 3:54
  • @PaulP—better read how String.prototype.substring works. It's not how you seem to expect. In particular, step 6: Let finalStart be min(max(intStart, 0), len).
    – RobG
    Apr 23, 2012 at 4:08
0
for (im=1;im<=31;im++){
  days[im] = (everyDay[im] < 10 ? '0' : '') + everyDay[im];
}
3
  • 3
    @RobG, Yes, I read the OP: "How would i use javascript or jquery..." Apr 23, 2012 at 4:09
  • I just want to see how you work jQuery into the answer. Hey, you got more rep for the reply comment than the answer! ;-)
    – RobG
    Apr 23, 2012 at 6:15
  • @RobG, strange. Here, have a fix of jQuery: $.each(everyDay, function (day, im) { if (1 <= im && im <= 31) { days[i] = (day < 10 ? '0' : '') + day; } }). Apr 23, 2012 at 6:57
0

If you want leading zeros in your days array. You can create another array with days as strings like this, and use one or the other where it belongs or use parseInt() on the new array:

var days = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31],
    strDays = [];

for (var i = 0, l = days.length; i < l; i++) {
    strDays.push(String(days[i]).length < 2 ? '0' + days[i] : String(days[i]));
}

// `strDays` prints: ["01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31"]

EDIT: Even shorter:

var strDays = [];
for (var i = 1; i < 32; i++) {
    strDays.push(('' + i).length < 2 ? '0' + i : '' + i);
}
0
for (var im=1;im<=31;im++){
     var x = parseInt(everyDay[im]);
     if(x < 10)
     days[im]='0' + x;
     else days[im]= x;
}
3
  • Now days contains some strings and some numbers : [, "01", "02", "03", ..., "08", "09", 10, 11, ..., 31]. Apr 23, 2012 at 6:58
  • @MikeSamuel I have updated my answer, Is it fine now? If not tell me the fault, I'l try to correct that one
    – Valli69
    Apr 23, 2012 at 7:13
  • @pravalika—there is no need for parseInt, an early step in the evaluation of the expression will be to convert x to a number primitive.
    – RobG
    Apr 24, 2012 at 0:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.