0

I'm using a calendar from jQueryUI datepicker.

For a some reason I would like to know, how to change a

data-month='" + printDate.getMonth() + "' 

to show current month with a leading zero, if it has it.

We may see this code (as a ready HTML) at a calendar's row:

<td class=" " data-handler="selectDay" data-event="click" data-month="8" data-year="2013">
    <a class="ui-state-default" href="#">13</a>
</td>

So, now it is a September, so it should shows a '09' instead of '8'

To make it '9' I did + (printDate.getMonth()+1) +

But I have no idea how to make it with a ZERO.

It should be something like

if(printDate.getMonth() < 10){printDate.getMonth() = '0'+printDate.getMonth()}

But this code does not work, I'm sure I did something wrong with it.

Uncaught ReferenceError: Invalid left-hand side in assignment

Thanks!

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
sergey_c
  • 741
  • 9
  • 14
  • http://stackoverflow.com/questions/3605214/javascript-add-leading-zeroes-to-date – Anagio Sep 23 '13 at 07:28
  • And http://stackoverflow.com/a/6979773/804087 – Anagio Sep 23 '13 at 07:29
  • while adding zero to printDate.getMonth() its converted to string, because of this its showing ReferenceError you need to use parseInt before assigning to it again – Greenhorn Sep 23 '13 at 07:33

1 Answers1

0

printDate.getMonth() is getter. You need to use setter or if there is not any store it in local variable for further use.

var monthString = printDate.getMonth();
if(printDate.getMonth() < 10){
    monthString= '0'+ printDate.getMonth();
}

And when generating HTML:

data-month='" + monthString + "' 
tomieq
  • 29
  • 1
  • 4