Advertisement
plirof2

Javascript show scheduled times and if they pass (eg for game rounds)

Apr 22nd, 2024
504
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <p id="schedule"></p>
  2.  
  3. <script>
  4. function displaySchedule(scheduleArray) {
  5.   var counter=1;
  6.     var currentTime = new Date(); // Get the current time
  7.     var html = '<ol style="list-style-type:none; padding: 0; margin: 0;">'; // Start ordered list markup with no bullet points and no padding/margin
  8.    
  9.     // Iterate over the schedule array
  10.     for (var i = 0; i < scheduleArray.length; i++) {
  11.         var scheduleTime = new Date();
  12.         var [hours, minutes] = scheduleArray[i].split(":");
  13.         scheduleTime.setHours(parseInt(hours, 10));
  14.         scheduleTime.setMinutes(parseInt(minutes, 10));
  15.         html +=(" ,<b>R"+(counter++)+":</b> ");
  16.         // Check if the schedule time is before the current time
  17.         if (scheduleTime < currentTime) {
  18.             html += '<li style="display:inline;"><del>' + scheduleArray[i] + '</del></li>'; // Strikethrough for times before current time
  19.         } else {
  20.             html += '<li style="display:inline;">' + scheduleArray[i] + '</li>'; // Regular formatting for times after or equal to current time
  21.         }
  22.        
  23.         // Add comma and space if it's not the last entry
  24.         if (i < scheduleArray.length - 1) {
  25.             html += ', ';
  26.         }
  27.     }
  28.    
  29.     html += '</ol>'; // End ordered list markup
  30.    
  31.     // Display the schedule in an element with id "schedule" (change the id as per your HTML)
  32.     document.getElementById("schedule").innerHTML = html;
  33. }
  34.  
  35. // Example usage:
  36. var scheduleArray = ["10:00", "10:45", "11:30", "12:15", "13:00", "13:45", "15:00","9:35"];
  37. displaySchedule(scheduleArray);
  38.  
  39.  
  40. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement