Jan 25

Leading Zero’s in jQueryUI’s Datepicker

Posted in General

Thought I’d share a quick tip to anyone who has to solve a similar problem. I was required to change the datepicker calendar to display days with leading zeros, something which currently none of the options or events will let you do to my knowledge. So after lots of digging through the source I came upon the quickest way to modify the calendar.

This involved doing a quick check during the output function, if the day was less than 10, prepend a ’0′ to the number, and then output the result.

 

Using jQueryUI version 1.8.17 (latest at time of this post) and the un-minified version of the source code, it involves a simple change to several lines and the result seems to work without issue.

Find line 9833, which should look like this:

var daySettings = (beforeShowDay ?

And add a new line before like this (the variable name is down to you of course)

var daynum = (printDate.getDate() < 10) ? ’0′ + printDate.getDate() : printDate.getDate();

Now we must change lines 9853

(unselectable ? ‘<span>’ + printDate.getDate() + ‘</span>’ : ‘<a class=”ui-state-default’ +

and 9857

‘” href=”#”>’ + printDate.getDate() + ‘</a>’)) + ‘</td>’; // display selectable date

Change both of these lines, swapping

printDate.getDate()

for

daynum

(replacing daynum with the name you used for the variable), and that’s that. Your calendar should now display days 1 to 9 with leading zeros!

comments: 0 »
Aug 11

Welcome to my Blog

Posted in General

So, my blog, here it is. As I learn and discover, I shall attempt to keep this updated with my findings, both interesting and unusual, helpful and insightful.

Please do keep tuned, I hope you will find what I have to say of use.

comments: Closed