DateHelper.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. *+------------------------------------------------------------------------+
  3. *| Licensed Materials - Property of IBM
  4. *| BI and PM: prmt
  5. *| (C) Copyright IBM Corp. 2002, 2013
  6. *|
  7. *| US Government Users Restricted Rights - Use, duplication or
  8. *| disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  9. *|
  10. *+------------------------------------------------------------------------+
  11. */
  12. /*
  13. DateHelper.js
  14. This script provides interactivity for the date prompt control
  15. */
  16. /*
  17. Constructor to create a new Date Picker Dialog / inline calendar
  18. oForm: a hidden form control to store the date
  19. oEditBox: the edit box that the user interacts with
  20. sRef: string, the name of this object
  21. sDefaultDate: a date to start the control
  22. iType: integer, 0= gregorian, 1= japanese imperial
  23. sInputOrder: YMD, DMY, ...
  24. iStartOfWeek: start day of the week. sunday = 0, monday = 1, ..., saturday = 6
  25. iDateTimeType: 0 = datetime e.g. yyyy-mm-dd 00:00:00.000, 1 = date e.g. yyyy-mm-dd
  26. sFirstDate: the minimum acceptable date
  27. sLastDate: the maximum acceptable date
  28. bRequired: boolean, specify if user input is required
  29. sCVId: string id
  30. */
  31. function PRMT_DateHelper( oForm, oEditBox, sRef, sDefaultDate, iType, sInputOrder, iStartOfWeek, iDateTimeType,
  32. sFirstDate, sLastDate, bRequired)
  33. {
  34. this.m_oForm = oForm;
  35. this.m_oEditBox = oEditBox;
  36. this.m_sRef = sRef;
  37. //check to see if a valid date was supplied
  38. this.m_sInputOrder = g_dateOrder;
  39. //restore a default value
  40. if (sDefaultDate)
  41. {
  42. //split off any time portions
  43. var sTestDate = sDefaultDate.split("T");
  44. //default date is in SQL format, yyyy-mm-dd
  45. this.m_dDate = dParseDate(sTestDate[0], "YMD");
  46. if ((this.m_dDate === false) || (!this.m_dDate))
  47. {
  48. this.m_dDate = new Date();
  49. }
  50. }
  51. else
  52. {
  53. this.m_dDate = new Date();
  54. }
  55. //sunday = 0, monday = 1, ..., saturday = 6
  56. this.m_iStartOfWeek = g_startDayOfWeek;
  57. //Calendar Type: 0=gregorian, 1=emperor
  58. this.m_iType = iType;
  59. //what the control should return to the form field
  60. //0 = datetime e.g. yyyy-mm-dd 00:00:00.000
  61. //1 = date e.g. yyyy-mm-dd
  62. this.m_iDateTimeType = iDateTimeType;
  63. //check to see if a valid first date was supplied
  64. if (sFirstDate)
  65. {
  66. this.m_dFirstDate = dParseDate(sFirstDate,"YMD");
  67. if (this.m_dFirstDate > this.m_dDate)
  68. {
  69. //the date is less than the first date,
  70. //reset to the first date
  71. this.m_dDate = this.m_dFirstDate;
  72. }
  73. }
  74. //check to see if a valid last date was supplied
  75. if (sLastDate)
  76. {
  77. this.m_dLastDate = dParseDate(sLastDate,"YMD");
  78. if (this.m_dLastDate < this.m_dDate)
  79. {
  80. //the date is greater than the last date,
  81. //reset to the last date
  82. this.m_dDate = this.m_dLastDate;
  83. }
  84. }
  85. //specify whether the user must enter a date or not
  86. this.m_bRequired = bRequired;
  87. this.m_bDisabled = false;
  88. //indicate whether the control has valid data
  89. this.m_bValid = false;
  90. };
  91. PRMT_DateHelper.prototype.getCurrentDate = function() { return this.m_dDate; };
  92. PRMT_DateHelper.prototype.f_checkDate = function(dateValue)
  93. {
  94. var bTestResult = true;
  95. //remove any non alpha numeric data
  96. var sTestDateStrip = sStripNonAlphanumerics(dateValue).f_trim();
  97. //remove the time if it is part of the string
  98. if (this.m_iDateTimeType === 0) {
  99. var rSplit = /[T]/;
  100. var arSplitValue = sTestDateStrip.split(rSplit);
  101. sTestDateStrip = arSplitValue[0];
  102. }
  103. var dTestDate = null;
  104. //is the field required? If not, a value does not have to be specified
  105. if ( !( this.m_bRequired === false && sTestDateStrip === K_PRMT_sEMPTY) ) {
  106. var iTestDate = false;
  107. //Fix for bug# 482564 - If this object is used for Date/Time dialog, specifically use YMD as date format
  108. var tempInputOrder = "YMD";
  109. if (this.m_iType === 1) {
  110. iTestDate = dParseEra(sTestDateStrip, tempInputOrder);
  111. } else {
  112. iTestDate = dParseDate(sTestDateStrip,tempInputOrder);
  113. }
  114. //check to see if this is a valid date
  115. if (!iTestDate) {//it is a number
  116. bTestResult = false;
  117. }
  118. //see if an acceptable date was supplied
  119. if ( bTestResult ) {
  120. dTestDate = iTestDate;
  121. //check to see if this date less than the first date OR
  122. //check to see if this date is greater than the last date
  123. //if so, then this is not a valid date
  124. if ( (this.m_dFirstDate && (dTestDate < this.m_dFirstDate)) ||
  125. (this.m_dLastDate && (dTestDate > this.m_dLastDate)) ) {
  126. bTestResult = false;
  127. }
  128. }
  129. }
  130. this.m_dDate = dTestDate;
  131. this.m_bValid = bTestResult;
  132. return bTestResult;
  133. };
  134. /**
  135. * Return a date string in the yyyy-mm-dd format
  136. *
  137. * @param Date dateValue
  138. */
  139. PRMT_DateHelper.prototype.f_formatDateYMD = function(dateValue)
  140. {
  141. var result = null;
  142. if (dateValue) {
  143. var nMonth = dateValue.getMonth() + 1;
  144. var sMonth = (nMonth > 9 ? String(nMonth) : "0" + nMonth);
  145. var nDay = dateValue.getDate();
  146. var sDay = (nDay > 9 ? String(nDay) : "0" + nDay);
  147. result = dateValue.getFullYear() + "-" + sMonth + "-" + sDay;
  148. }
  149. return result;
  150. }
  151. //return the content locale formatted value
  152. PRMT_DateHelper.prototype.f_formatDateCurrentLocale = function(dateObj)
  153. {
  154. return getFormatDate (dateObj, this.m_iType, this.m_sInputOrder);
  155. }
  156. //take the current date and update it
  157. PRMT_DateHelper.prototype.f_formatSubmitDate = function(dNewDate)
  158. {
  159. var result;
  160. //Is there a date?
  161. if (dNewDate) {
  162. //convert to the new value and update the form
  163. //note that this value needs to be properly formatted
  164. //where dates need to be fully specified
  165. var sNewYear = dNewDate.getFullYear();
  166. //add extra 0(s) if necessary
  167. sNewYear = "0000" + sNewYear;
  168. sNewYear = sNewYear.substring(sNewYear.length-4, sNewYear.length);
  169. var sNewMonth = (parseInt(dNewDate.getMonth(), 10) + 1).toString();//+need to add one;
  170. //add an extra 0 if necessary
  171. if (sNewMonth.length == 1) {
  172. sNewMonth = "0" + sNewMonth;
  173. }
  174. var sNewDay = (dNewDate.getDate()).toString();
  175. //add an extra 0 if necessary
  176. if (sNewDay.length == 1) {
  177. sNewDay = "0" + sNewDay;
  178. }
  179. var sNewValue = sNewYear + "-" + sNewMonth + "-" + sNewDay;
  180. //note that this value needs to be properly formatted
  181. //where dates need to be fully specified
  182. if (this.m_iDateTimeType === 0) {
  183. sNewValue += "T00:00:00.000";
  184. }
  185. result = sNewValue;
  186. } else {
  187. //remove the value from the form
  188. result = K_PRMT_sEMPTY;
  189. }
  190. return result;
  191. }