Finding Date/Time Columns on the Page with g_strDateTimeControlIDs

I ran across an interesting little trick in some code I’m working on today.  As usual, not my code, someone else’s that I’m trying to decipher and fix.

One thing that they are doing is sort of slick.  There’s apparently an array created by the datepicker.js JavaScript called g_strDateTimeControlID which is populated with the IDs for all of the date input fields on the page which utilize the Date Picker. (I did some Binging, and there are only 4 references to g_strDateTimeControlID that I found.)  The little calendar icon below is the Date Picker:


You can use the g_strDateTimeControlIDs array like this:

var eventDateID = g_strDateTimeControlIDs["SPEventDate"];
document.getElementById(eventDateID).value = date;

Note the ‘SP’ ahead of the column name.

That’s certainly simpler than writing your own JavaScript/jQuery to find the control, and makes it easy to set the date value, whether it be from a Query String value or some calculation.  Because the array is constructed and used by the Date Picker, so it should be available on the page where ever the Date Picker is seen.