PageSourceSearch

https://gpu123.com/Scripts/datePicker.js

js gpu123.com collected 2026-10-07 22:16:03 UTC 18,688 bytes, 522 lines download raw bytes

1 
2var dayArrayShort;
3var dayArrayMed ;
4var dayArrayLong;
5var monthArrayShort;
6var monthArrayMed;
7var monthArrayLong;
8
9
10var todayText = "今日";
11var closeText = "關閉"
12
13var sundayShortText    = "日";
14var mondayShortText = "一";
15var tuesdayShortText = "二";
16var wednesdayShortText = "三";
17var thursdayShortText = "四";
18var fridayShortText = "五";
19var saturdayShortText = "六";
20
21var sundayMediumText = "日";
22var mondayMediumText = "一";
23var tuesdayMediumText = "二";
24var wednesdayMediumText = "三";
25var thursdayMediumText = "四";
26var fridayMediumText = "五";
27var saturdayMediumText = "六";
28
29var sundayText = "星期日";
30var mondayText = "星期一";
31var tuesdayText = "星期二";
32var wednesdayText = "星期三";
33var thursdayText = "星期四";
34var fridayText = "星期五";
35var saturdayText = "星期六";
36
37var januaryText = "一月";
38var februaryText = "二月";
39var marchText = "三月";
40var aprilText = "四月";
41var mayText = "五月";
42var juneText = "六月";
43var julyText = "七月";
44var augustText = "八月";
45var septemberText = "九月";
46var octoberText = "十月";
47var novemberText = "十一月";
48var decemberText = "十二月";
49
50
51function InitializeDatePicker()
52{
53    datePickerDivID = "datepicker";
54    iFrameDivID = "datepickeriframe";
55
56//var dayArrayShort = new Array('Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa');
57    dayArrayShort = new Array(sundayShortText, mondayShortText, tuesdayShortText, wednesdayShortText, thursdayShortText, fridayShortText, saturdayShortText);
58
59//var dayArrayMed = new Array('Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat');
60    dayArrayMed = new Array(sundayMediumText, mondayMediumText, tuesdayMediumText, wednesdayShortText, wednesdayMediumText, fridayMediumText, saturdayMediumText);
61    //var dayArrayLong = new Array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
62
63    dayArrayLong = new Array(sundayText, mondayText, tuesdayText, wednesdayText, thursdayText, fridayText, saturdayText);
64//var monthArrayShort = new Array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
65    //monthArrayShort = new Array('一', '二', '三', '四', '五', '六', '七', '八', '九', '十', '十一', '十二');
66//var monthArrayMed = new Array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'June', 'July', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec');
67    //monthArrayMed = new Array('一', '二', '三', '四', '五', '六', '七', '八', '九', '十', '十一', '十二');
68//var monthArrayLong = new Array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
69    monthArrayLong = new Array(januaryText, februaryText, marchText, aprilText, mayText, juneText, julyText, augustText, septemberText, octoberText, novemberText, decemberText);
70
71 // these variables define the date formatting we're expecting and outputting.
72// If you want to use a different format by default, change the defaultDateSeparator
73// and defaultDateFormat variables either here or on your HTML page.
74    defaultDateSeparator = "/";        // common values would be "/" or "."
75    defaultDateFormat = "mdy"    // valid values are "mdy", "dmy", and "ymd"
76    dateSeparator = defaultDateSeparator;
77    dateFormat = defaultDateFormat;
78}
79
80//function displayDatePicker(dateFieldName, displayBelowThisObject, dtFormat, dtSep)
81function displayDatePicker(dateFieldElement, displayBelowThisObject, dtFormat, dtSep)
82{
83  //var targetDateField = document.getElementsByName (dateFieldName).item(0);
84  //var dateFieldName = dateFieldElement.name;
85  var targetDateField = dateFieldElement;
86 
87
88    if( dateFieldElement.readOnly == true )
89    {
90        return false;
91    }
92    
93    InitializeDatePicker()
94
95  if ( dateFieldElement == null && displayBelowThisObject != null )
96  {
97    targetDateField = document.all(displayBelowThisObject.sourceIndex-1);
98    displayBelowThisObject = targetDateField ;
99  }
100   
101  // if we weren't told what node to display the datepicker beneath, just display it
102  // beneath the date field we're updating
103  if (!displayBelowThisObject)
104    displayBelowThisObject = targetDateField;
105 
106  // if a date separator character was given, update the dateSeparator variable
107  if (dtSep)
108    dateSeparator = dtSep;
109  else
110    dateSeparator = defaultDateSeparator;
111 
112  // if a date format was given, update the dateFormat variable
113  if (dtFormat)
114    dateFormat = dtFormat;
115  else
116    dateFormat = defaultDateFormat;
117 
118  var x = displayBelowThisObject.offsetLeft;
119  var y = displayBelowThisObject.offsetTop + displayBelowThisObject.offsetHeight ;
120 
121  // deal with elements inside tables and such
122  var parent = displayBelowThisObject;
123  while (parent.offsetParent) {
124    parent = parent.offsetParent;
125    x += parent.offsetLeft;
126    y += parent.offsetTop ;
127  }
128 
129  drawDatePicker(targetDateField, x, y);
130}
131 
132function drawDatePicker(targetDateField, x, y)
133{
134  var dt = getFieldDate(targetDateField.value );
135 
136  // the datepicker table will be drawn inside of a <div> with an ID defined by the
137  // global datePickerDivID variable. If such a div doesn't yet exist on the HTML
138  // document we're working with, add one.
139  if (!document.getElementById(datePickerDivID)) {
140    // don't use innerHTML to update the body, because it can cause global variables
141    // that are currently pointing to objects on the page to have bad references
142    //document.body.innerHTML += "<div id='" + datePickerDivID + "' class='dpDiv'></div>";
143    var newNode = document.createElement("div");
144    newNode.setAttribute("id", datePickerDivID);
145    newNode.setAttribute("class", "dpDiv");
146    newNode.setAttribute("style", "visibility: hidden;");
147    document.body.appendChild(newNode);
148  }
149 
150  // move the datepicker div to the proper x,y coordinate and toggle the visiblity
151
152  var pickerDiv = document.getElementById(datePickerDivID);
153
154  pickerDiv.style.position = "absolute";
155  pickerDiv.style.left = x + "px";
156  pickerDiv.style.top = y + "px";
157  pickerDiv.style.visibility = (pickerDiv.style.visibility == "visible" ? "hidden" : "visible");
158  pickerDiv.style.display = (pickerDiv.style.display == "block" ? "none" : "block");
159  pickerDiv.style.zIndex = 10000;
160 
161  // draw the datepicker table
162  refreshDatePicker(targetDateField.name, dt.getFullYear(), dt.getMonth(), dt.getDate());
163}
164
165
166/**
167This is the function that actually draws the datepicker calendar.
168*/
169function refreshDatePicker(dateFieldName, year, month, day)
170{
171  // if no arguments are passed, use today's date; otherwise, month and year
172  // are required (if a day is passed, it will be highlighted later)
173  var thisDay = new Date();
174 
175  if ((month >= 0) && (year > 0)) {
176    thisDay = new Date(year, month, 1);
177  } else {
178    day = thisDay.getDate();
179    thisDay.setDate(1);
180  }
181 
182  // the calendar will be drawn as a table
183  // you can customize the table elements with a global CSS style sheet,
184  // or by hardcoding style and formatting elements below
185  var crlf = "\r\n";
186  var TABLE = "<table cols=7 class='dpTable'>" + crlf;
187  var xTABLE = "</table>" + crlf;
188  var TR = "<tr class='dpTR'>";
189  var TR_title = "<tr class='dpTitleTR'>";
190  var TR_days = "<tr class='dpDayTR'>";
191  var TR_todaybutton = "<tr class='dpTodayButtonTR'>";
192  var xTR = "</tr>" + crlf;
193  var TD = "<td class='dpTD' onMouseOut='this.className=\"dpTD\";' onMouseOver=' this.className=\"dpTDHover\";' ";    // leave this tag open, because we'll be adding an onClick event
194  var TD_title = "<td colspan=5 class='dpTitleTD'>";
195  var TD_buttons = "<td class='dpButtonTD'>";
196  var TD_todaybutton = "<td colspan=7 class='dpTodayButtonTD'>";
197  var TD_days = "<td class='dpDayTD'>";
198  var TD_selected = "<td class='dpDayHighlightTD' onMouseOut='this.className=\"dpDayHighlightTD\";' onMouseOver='this.className=\"dpTDHover\";' ";    // leave this tag open, because we'll be adding an onClick event
199  var xTD = "</td>" + crlf;
200  var DIV_title = "<div class='dpTitleText'>";
201  var DIV_selected = "<div class='dpDayHighlight'>";
202  var xDIV = "</div>";
203 
204  // start generating the code for the calendar table
205  var html = TABLE;
206 
207  // this is the title bar, which displays the month and the buttons to
208  // go back to a previous month or forward to the next month
209  html += TR_title;
210  html += TD_buttons + getButtonCode(dateFieldName, thisDay, -1, "&lt;") + xTD;
211  html += TD_title + DIV_title + monthArrayLong[ thisDay.getMonth()] + " " + thisDay.getFullYear() + xDIV + xTD;
212  html += TD_buttons + getButtonCode(dateFieldName, thisDay, 1, "&gt;") + xTD;
213  html += xTR;
214 
215  // this is the row that indicates which day of the week we're on
216  html += TR_days;
217  for(i = 0; i < dayArrayShort.length; i++)
218    html += TD_days + dayArrayShort[i] + xTD;
219  html += xTR;
220 
221  // now we'll start populating the table with days of the month
222  html += TR;
223 
224  // first, the leading blanks
225  for (i = 0; i < thisDay.getDay(); i++)
226    html += TD + "&nbsp;" + xTD;
227 
228  // now, the days of the month
229  do {
230    dayNum = thisDay.getDate();
231    TD_onclick = " onclick=\"updateDateField('" + dateFieldName + "', '" + getDateString(thisDay) + "');\">";
232    
233    if (dayNum == day)
234      html += TD_selected + TD_onclick + DIV_selected + dayNum + xDIV + xTD;
235    else
236      html += TD + TD_onclick + dayNum + xTD;
237    
238    // if this is a Saturday, start a new row
239    if (thisDay.getDay() == 6)
240      html += xTR + TR;
241    
242    // increment the day
243    thisDay.setDate(thisDay.getDate() + 1);
244  } while (thisDay.getDate() > 1)
245 
246  // fill in any trailing blanks
247  if (thisDay.getDay() > 0) {
248    for (i = 6; i > thisDay.getDay(); i--)
249      html += TD + "&nbsp;" + xTD;
250  }
251  html += xTR;
252 
253  // add a button to allow the user to easily return to today, or close the calendar
254  var today = new Date();
255  //var todayString = "Today is " + dayArrayMed[today.getDay()] + ", " + monthArrayMed[ today.getMonth()] + " " + today.getDate();
256  html += TR_todaybutton + TD_todaybutton;
257  html += "<button style='width:60px' class='dpTodayButton' onClick='refreshDatePicker(\"" + dateFieldName + "\");'>" + todayText + "</button> ";
258  html += "&nbsp; <button style='width:60px' class='dpTodayButton' onClick='updateDateField(\"" + dateFieldName + "\");'>"  + closeText + "</button>";
259  html += xTD + xTR;
260 
261  // and finally, close the table
262  html += xTABLE;
263 
264  document.getElementById(datePickerDivID).innerHTML = html;
265  // add an "iFrame shim" to allow the datepicker to display above selection lists
266  adjustiFrame();
267}
268
269
270/**
271Convenience function for writing the code for the buttons that bring us back or forward
272a month.
273*/
274function getButtonCode(dateFieldName, dateVal, adjust, label)
275{
276  var newMonth = (dateVal.getMonth () + adjust) % 12;
277  var newYear = dateVal.getFullYear() + parseInt((dateVal.getMonth() + adjust) / 12);
278  if (newMonth < 0) {
279    newMonth += 12;
280    newYear += -1;
281  }
282 
283  return "<button style='width:40px' class='dpButton' onClick='refreshDatePicker(\"" + dateFieldName + "\", " + newYear + ", " + newMonth + ");'>" + label + "</button>";
284}
285
286
287/**
288Convert a JavaScript Date object to a string, based on the dateFormat and dateSeparator
289variables at the beginning of this script library.
290*/
291function getDateString(dateVal)
292{
293/*
294  var dayString = "00" + dateVal.getDate();
295  var monthString = "00" + (dateVal.getMonth()+1);
296  dayString = dayString.substring(dayString.length - 2);
297  monthString = monthString.substring(monthString.length - 2);
298 
299  switch (dateFormat) {
300    case "dmy" :
301      return dayString + dateSeparator + monthString + dateSeparator + dateVal.getFullYear();
302    case "ymd" :
303      return dateVal.getFullYear() + dateSeparator + monthString + dateSeparator + dayString;
304    case "mdy" :
305    default :
306      return monthString + dateSeparator + dayString + dateSeparator + dateVal.getFullYear();
307  }
308 */
309 var year = 0;
310
311//alert( "dateVal.getYear() =" + dateVal.getYear() );
312
313 //year = dateVal.getYear() + 1900;
314
315year = dateVal.getFullYear() ;
316 
317 return year + "/" +(dateVal.getMonth()+1) + "/" + dateVal.getDate(); 
318}
319
320
321/**
322Convert a string to a JavaScript Date object.
323*/
324function getFieldDate(dateString)
325{
326  var dateVal;
327  var dArray;
328  var d, m, y;
329  var date;
330
331     dateVal = new Date(dateString);
332    
333     
334    //dateVal = date.getYear() + "/" + (date.getMonth() + 1) + "/" + date.getDate();
335 /*  
336  try {
337    dArray = splitDateString(dateString);
338    if (dArray) {
339      switch (dateFormat) {
340        case "dmy" :
341          d = parseInt(dArray[0], 10);
342          m = parseInt(dArray[1], 10) - 1;
343          y = parseInt(dArray[2], 10);
344          break;
345        case "ymd" :
346          d = parseInt(dArray[2], 10);
347          m = parseInt(dArray[1], 10) - 1;
348          y = parseInt(dArray[0], 10);
349          break;
350        case "mdy" :
351        default :
352          d = parseInt(dArray[1], 10);
353          m = parseInt(dArray[0], 10) - 1;
354          y = parseInt(dArray[2], 10);
355          break;
356      }
357      dateVal = new Date(y, m, d);
358    } else if (dateString) {
359      dateVal = new Date(dateString);
360    } else {
361      dateVal = new Date();
362    }
363  } catch(e) {
364    dateVal = new Date();
365  }
366*/
367 
368   return dateVal;
369}
370
371
372/**
373Try to split a date string into an array of elements, using common date separators.
374If the date is split, an array is returned; otherwise, we just return false.
375*/
376function splitDateString(dateString)
377{
378  var dArray;
379  if (dateString.indexOf("/") >= 0)
380    dArray = dateString.split("/");
381  else if (dateString.indexOf(".") >= 0)
382    dArray = dateString.split(".");
383  else if (dateString.indexOf("-") >= 0)
384    dArray = dateString.split("-");
385  else if (dateString.indexOf("\\") >= 0)
386    dArray = dateString.split("\\");
387  else
388    dArray = false;
389 
390  return dArray;
391}
392
393/**
394Update the field with the given dateFieldName with the dateString that has been passed,
395and hide the datepicker. If no dateString is passed, just close the datepicker without
396changing the field value.
397
398Also, if the page developer has defined a function called datePickerClosed anywhere on
399the page or in an imported library, we will attempt to run that function with the updated
400field as a parameter. This can be used for such things as date validation, setting default
401values for related fields, etc. For example, you might have a function like this to validate
402a start date field:
403
404function datePickerClosed(dateField)
405{
406  var dateObj = getFieldDate(dateField.value);
407  var today = new Date();
408  today = new Date(today.getFullYear(), today.getMonth(), today.getDate());
409 
410  if (dateField.name == "StartDate") {
411    if (dateObj < today) {
412      // if the date is before today, alert the user and display the datepicker again
413      alert("Please enter a date that is today or later");
414      dateField.value = "";
415      document.getElementById(datePickerDivID).style.visibility = "visible";
416      adjustiFrame();
417    } else {
418      // if the date is okay, set the EndDate field to 7 days after the StartDate
419      dateObj.setTime(dateObj.getTime() + (7 * 24 * 60 * 60 * 1000));
420      var endDateField = document.getElementsByName ("EndDate").item(0);
421      endDateField.value = getDateString(dateObj);
422    }
423  }
424}
425
426*/
427function updateDateField(dateFieldName, dateString)
428{    
429  var targetDateField = document.getElementsByName (dateFieldName).item(0);
430   
431   
432   
433  if (dateString)
434  {
435    targetDateField.value = dateString;
436  }
437   
438  var pickerDiv = document.getElementById(datePickerDivID);
439  pickerDiv.style.visibility = "hidden";
440  pickerDiv.style.display = "none";
441 
442  adjustiFrame();
443  targetDateField.focus();
444 
445  // after the datepicker has closed, optionally run a user-defined function called
446  // datePickerClosed, passing the field that was just updated as a parameter
447  // (note that this will only run if the user actually selected a date from the datepicker)
448  if ((dateString) && (typeof(datePickerClosed) == "function"))
449  {
450      datePickerClosed(targetDateField);
451  }
452
453
454  if (dateString)
455  {
456     OnNodeValueChange(targetDateField);
457     
458    
459     if( targetDateField.onchange_handler != null && targetDateField.onchange_handler != "" )
460     {
461         handler = targetDateField.onchange_handler.replace("this","targetDateField");
462        
463         eval( handler );
464     }
465  }
466}
467
468
469/**
470Use an "iFrame shim" to deal with problems where the datepicker shows up behind
471selection list elements, if they're below the datepicker. The problem and solution are
472described at:
473
474http://dotnetjunkies.com/WebLog/jking/archive/2003/07/21/488.aspx
475http://dotnetjunkies.com/WebLog/jking/archive/2003/10/30/2975.aspx
476*/
477function adjustiFrame(pickerDiv, iFrameDiv)
478{
479  // we know that Opera doesn't like something about this, so if we
480  // think we're using Opera, don't even try
481  var is_opera = (navigator.userAgent.toLowerCase().indexOf("opera") != -1);
482  if (is_opera)
483    return;
484  
485  // put a try/catch block around the whole thing, just in case
486  try {
487    if (!document.getElementById(iFrameDivID)) {
488      // don't use innerHTML to update the body, because it can cause global variables
489      // that are currently pointing to objects on the page to have bad references
490      //document.body.innerHTML += "<iframe id='" + iFrameDivID + "' src='javascript:false;' scrolling='no' frameborder='0'>";
491      var newNode = document.createElement("iFrame");
492      newNode.setAttribute("id", iFrameDivID);
493      newNode.setAttribute("src", "javascript:false;");
494      newNode.setAttribute("scrolling", "no");
495      newNode.setAttribute ("frameborder", "0");
496      document.body.appendChild(newNode);
497    }
498    
499    if (!pickerDiv)
500      pickerDiv = document.getElementById(datePickerDivID);
501    if (!iFrameDiv)
502      iFrameDiv = document.getElementById(iFrameDivID);
503    
504    try {
505      iFrameDiv.style.position = "absolute";
506      iFrameDiv.style.width = pickerDiv.offsetWidth;
507      iFrameDiv.style.height = pickerDiv.offsetHeight ;
508      iFrameDiv.style.top = pickerDiv.style.top;
509      iFrameDiv.style.left = pickerDiv.style.left;
510      iFrameDiv.style.zIndex = pickerDiv.style.zIndex - 1;
511      iFrameDiv.style.visibility = pickerDiv.style.visibility ;
512      iFrameDiv.style.display = pickerDiv.style.display;
513    } catch(e) {
514    }
515 
516  } catch (ee) {
517  }
518 
519}
520
521InitializeDatePicker();
522 

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.