test.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. var ShiftStrings = ["Shift 1 (23:00 - 07:00)", "Shift 2 (07:00 - 15:00)", "Shift 3 (15:00 - 23:00)"];
  2. var ShiftTimes;
  3. var CurrentDate;
  4. var SelectedDate;
  5. var CurrentShift;
  6. var SelectedShift;
  7. function formatDate(date) {
  8. var d = new Date(date),
  9. month = '' + (d.getMonth() + 1),
  10. day = '' + d.getDate(),
  11. year = d.getFullYear();
  12. if (month.length < 2) month = '0' + month;
  13. if (day.length < 2) day = '0' + day;
  14. return [year, month, day].join('-');
  15. }
  16. function SetCurrentDate() {
  17. var datetime = new Date();
  18. if (datetime.getHours() == 23) {
  19. datetime.setDate(datetime.getDate() + 1);
  20. }
  21. CurrentDate = formatDate(datetime);
  22. if ($('#ShiftDate').length) {
  23. SelectedDate = $('#ShiftDate').val();
  24. } else {
  25. SelectedDate = CurrentDate;
  26. }
  27. }
  28. function SetShiftTimes() {
  29. var datetime = new Date();
  30. if (datetime.getHours() == 23 || datetime.getHours() < 7) {
  31. ShiftTimes = Times[0];
  32. CurrentShift = 0;
  33. } else if (datetime.getHours() < 15) {
  34. CurrentShift = 1;
  35. } else {
  36. CurrentShift = 2;
  37. }
  38. if ($('#Shift').length) {
  39. SelectedShift = ShiftStrings.indexOf($('#Shift').val());
  40. } else {
  41. SelectedShift = CurrentShift;
  42. }
  43. }
  44. SetShiftTimes();
  45. SetCurrentDate();
  46. $('#prev, #today, #next').on('click', function() {
  47. console.log("Shift Before:" + SelectedShift);
  48. if (this.id == "prev") {
  49. SelectedShift = SelectedShift - 1;
  50. if (SelectedShift < 0) {
  51. SelectedShift = 2;
  52. IncrementDate(-1);
  53. }
  54. } else if (this.id == "next") {
  55. SelectedShift = SelectedShift + 1;
  56. if (SelectedShift > 2) {
  57. SelectedShift = 0;
  58. IncrementDate(1);
  59. }
  60. } else {
  61. SelectedShift = CurrentShift;
  62. SelectedDate = CurrentDate;
  63. }
  64. document.getElementById('date').innerHTML = "Date: " + SelectedDate;
  65. document.getElementById('shift').innerHTML = "Shift: " + SelectedShift;
  66. function IncrementDate(i) {
  67. console.log("SelectedDate Before:" + SelectedDate);
  68. var tmpdate = new Date(SelectedDate);
  69. console.log("tmpDate Before:" + tmpdate);
  70. console.log("tmpDate Before:" + formatDate(tmpdate));
  71. tmpdate.setDate(tmpdate.getDate() + i);
  72. console.log("tmpDate After:" + tmpdate);
  73. console.log("tmpDate After:" + formatDate(tmpdate));
  74. SelectedDate = formatDate(tmpdate);
  75. console.log("SelectedDate After:" + SelectedDate);
  76. }
  77. });
  78. document.getElementById('date').innerHTML = "Date: " + SelectedDate;
  79. document.getElementById('shift').innerHTML = "Shift: " + SelectedShift;