am_dot_com

SW 2022-04-06

Apr 6th, 2022 (edited)
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.39 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>About Date...</title>
  6. <script src="amutil.js"></script><!-- promotes code reuse -->
  7. <script src="about_dates.js"></script><!-- project specific -->
  8. </head>
  9. <body>
  10. <!-- form for users to input year, month, day -->
  11. <form
  12. id="idFormDate"
  13. >
  14. <fieldset>
  15. <legend>Your date:</legend>
  16. <label for="idYear">Year:</label>
  17. <input type="number" min="1" id="idYear" value="1969">
  18. <br>
  19. <label for="idMonth">Month:</label>
  20. <input type="number" min="1" max="12" value="6" id="idMonth">
  21. <br>
  22. <label for="idDay">Day:</label>
  23. <input type="number" min="1" max="31" value="21" id="idDay">
  24. </fieldset>
  25. <input type="submit" value="confirm date">
  26. </form>
  27. <hr>
  28. <section id="idSectionFeedback"></section>
  29. </body>
  30. </html>
  31.  
  32. ***********************
  33.  
  34. window.onload=boot;
  35.  
  36. const DAYS_OF_WEEK = [
  37. "Domingo", "Segunda", "Terça", "Quarta",
  38. "Quinta", "Sexta", "Sábado"
  39. ]
  40.  
  41. const
  42. ID_YEAR="idYear",
  43. ID_MONTH="idMonth",
  44. ID_DAY="idDay",
  45. ID_FORM_DATE="idFormDate",
  46. ID_SECTION_FEEDBACK="idSectionFeedback";
  47.  
  48. var oYear, oMonth, oDay, oFormDate, oSectionFeedback;
  49.  
  50. function boot(){
  51. //1 - associations/bridging HTML to JS
  52. oYear=id(ID_YEAR);
  53. oMonth=id(ID_MONTH);
  54. oDay=id(ID_DAY);
  55. oFormDate=id(ID_FORM_DATE);
  56. oSectionFeedback=id(ID_SECTION_FEEDBACK);
  57. /*
  58. var theRelevant = new Array();
  59. theRelevant.push(oYear);
  60. */
  61. var theRelevant = [
  62. oYear, oMonth, oDay, oFormDate, oSectionFeedback
  63. ]
  64.  
  65. //quality control
  66. var bQC = qualityControl(theRelevant);
  67.  
  68. if (!bQC){
  69. window.alert("Critical Error. System halted.");
  70. exit();
  71. }
  72. else{
  73. //v1
  74. //window.alert("System OK, can proceed.");
  75.  
  76. //v2
  77. //NOTHING
  78. }
  79.  
  80. //2 - set behaviors
  81. oFormDate.onsubmit = ehDoSomethingWithTheDate;
  82. //oFormDate.onsubmit = theFunctionThatReallyDoesTheJob;
  83.  
  84. oYear.onchange = oMonth.onchange = oDay.onchange =
  85. ehDoSomethingWithTheDate;
  86.  
  87. ehDoSomethingWithTheDate();
  88. }//boot
  89.  
  90. /*
  91. function ehDoSomethingWithTheDate(){
  92. return theFunctionThatReallyDoesTheJob;
  93. }
  94.  
  95. function theFunctionThatReallyDoesTheJob(){
  96.  
  97. }
  98. */
  99.  
  100. function ehDoSomethingWithTheDate(){
  101. const iYear = Number(oYear.value);
  102. const iMonth = Number(oMonth.value);
  103. const iDay = Number(oDay.value);
  104.  
  105. /*
  106. var o1 = new Date(); //agora!
  107. var o2 = new Date(2022, 12-1, 25, 0, 0, 0)
  108. */
  109. var theUserDate = new Date(
  110. iYear,
  111. iMonth-1,
  112. iDay
  113. );
  114. var zeroBasedDayOfWeek = theUserDate.getDay();
  115.  
  116. //v1
  117. //oSectionFeedback.innerHTML = zeroBasedDayOfWeek;
  118.  
  119. //v2
  120. var strDayOfWeek =
  121. DAYS_OF_WEEK[zeroBasedDayOfWeek];
  122.  
  123. var distance = distanceBetweenDatesInMultipleUnits(
  124. theUserDate,
  125. new Date()
  126. );
  127. var simplifiedDistance =
  128. distance['d'].toFixed(2)
  129.  
  130. strDistance = simplifiedDistance +
  131. " is day(s) distant.";
  132. oSectionFeedback.innerHTML = strDayOfWeek +
  133. "<br>"+strDistance;
  134.  
  135. return false; //stopped the event bubbling up
  136. /*
  137. no more query string formation
  138. no document reload
  139. so, the result was kept visible
  140. */
  141. }
  142.  
  143. function distanceBetweenDatesInMultipleUnits(
  144. pD1, //the 1st of the dates
  145. pD2 //the 2nd of the dates
  146. ){
  147. //bad approach
  148. //var d = pD2.getMilliseconds() - pD1.getMilliseconds();
  149.  
  150. var distanceInMs = pD2-pD1;
  151. var distanceInSec = distanceInMs/1000;
  152. var distanceInMinutes = distanceInSec/60;
  153. var distanceInHours = distanceInMinutes/60;
  154. var distanceInDays = distanceInHours/24;
  155.  
  156. //v1
  157. //return distanceInDays;
  158.  
  159. //v2
  160. var oJSON = {
  161. "ms" : distanceInMs,
  162. "s" : distanceInSec,
  163. "m" : distanceInMinutes,
  164. "h" : distanceInHours,
  165. "d" : distanceInDays
  166. }
  167. return oJSON;
  168. }//distanceBetweenDatesInMultipleUnits
  169.  
  170.  
  171.  
  172.  
  173.  
  174.  
  175.  
  176. ****************
  177. function id(pId){
  178. return document.getElementById(pId);
  179. }//id
  180.  
  181. //_.~^~._.~^~._.~^~._.~^~._.~^~._.~^~._.~^~._.~^~._.~^~._.~^~.
  182. function qualityControl(pColRelevant){
  183. for (o of pColRelevant) {
  184. if (o == null){
  185. return false;
  186. }//if
  187. }//for
  188. return true;
  189. }//qualityControl
  190.  
  191.  
  192.  
Advertisement
Add Comment
Please, Sign In to add comment