LimitTimeBetweenTwoDates.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. define( function() {
  2. "use strict";
  3. function DateRangePrompt()
  4. {
  5. };
  6. function strDateToDateObj (dateValue){
  7. var dateYear = dateValue.substr(0,4);
  8. var dateMonth=dateValue.substr(5,2);
  9. var dateDay=dateValue.substr(8,2);
  10. var result = new Date(dateYear, dateMonth-1, dateDay);
  11. return result;
  12. }
  13. function substractDate(endDate, startDate) {
  14. var startDateObj = strDateToDateObj(startDate);
  15. var endDateObj = strDateToDateObj(endDate);
  16. var diffInMS = endDateObj.getTime() - startDateObj.getTime();
  17. return Math.round(diffInMS != 0? diffInMS/(24 * 3600 * 1000) : 0);
  18. }
  19. DateRangePrompt.prototype.draw = function( oControlHost )
  20. {
  21. var e1=oControlHost.container;
  22. e1.innerHTML =
  23. '<style>' +
  24. '.MyPromptButton' +
  25. '{' +
  26. 'background-color: white;' +
  27. 'border: 2px solid #4178BE;' +
  28. 'color: #4178be;' +
  29. 'font-size: 14px;' +
  30. 'height: 35px;' +
  31. 'width: 120px;' +
  32. 'margin-left: 20px;' +
  33. 'cursor: pointer;' +
  34. '} ' +
  35. '.MyPromptButton:hover' +
  36. '{' +
  37. 'background-color: #4178BE;' +
  38. 'color: white;' +
  39. '}' +
  40. '</style>' +
  41. '<button class="MyPromptButton btn1">Run Report</button>';
  42. e1.querySelector(".btn1").onclick = this.MyPromptButtonFinishClick.bind(this, oControlHost);
  43. };
  44. DateRangePrompt.prototype.MyPromptButtonFinishClick = function (oControlHost)
  45. {
  46. var dateRangePrompt = oControlHost.page.getControlByName("myDateRange");
  47. var rangeLimit = 10;
  48. if (dateRangePrompt)
  49. {
  50. // Create an array of date prompt values
  51. var aDatePromptValues = dateRangePrompt.getValues();
  52. //Get the first value from the array
  53. var rangeValue = aDatePromptValues[0];
  54. //Initialize a variable to hold the number of days between the two dates
  55. var rangeDaysDiff = 0;
  56. // If the range start and end date set, then validate it
  57. if (rangeValue.start && rangeValue.end) {
  58. // Get the start and end dates as strings
  59. var startDate = rangeValue.start.use;
  60. var endDate = rangeValue.end.use;
  61. // Convert string prompt values to proper dates
  62. var startDateObj = strDateToDateObj(startDate);
  63. var endDateObj = strDateToDateObj(endDate);
  64. // Calculate the difference in days between the dates
  65. rangeDaysDiff = substractDate(endDate,startDate);
  66. // The .getTime method, calculates the time in milliseconds between date and 01 January 1970.
  67. // Determine the difference between the dates in milliseconds
  68. var diffInMS = endDateObj.getTime() - startDateObj.getTime();
  69. // If (milliseconds time difference is not zero)
  70. // then result = (the difference in days)
  71. // else 0.
  72. rangeDaysDiff = Math.round(diffInMS != 0? diffInMS/(24 * 3600 * 1000) : 0);
  73. if (rangeDaysDiff >= 0 && rangeDaysDiff <= rangeLimit)
  74. {
  75. oControlHost.finish()
  76. } else
  77. {
  78. alert ("The dates selected cannot be more than " + rangeLimit + " days apart. Please select a shorter date range.");
  79. }
  80. }
  81. }
  82. };
  83. return DateRangePrompt;
  84. });