Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I don't think a date library will help much here, it will just add bloat and something else to maintain and worry about. The following gets the "current" activities, it's up to you to format the returned data and do something with it.</p> <p>It doesn't need much date support, it's mostly filtering the activities. Converting the times to date objects for comparison is trivial (3 lines of code).</p> <pre><code>var dUtil = { // Use provided date object, or today // Add support for strings? getDayName: function(dateObj) { var days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']; dateObj = dateObj || new Date(); return days[dateObj.getDay()]; }, // Expects a string like '10:00 AM', assumes current date // Note that this will only work where client and server are // the same timezone timeToDate: function(s) { var d = new Date(); s = s.split(/[: ]/); d.setHours((s[2] == 'AM'? s[0] : +s[0] + 12), s[1], 0); return d; } }; function getCurrentActivities(data) { var currentActivities = []; var todayActivities, activity, name, times, start, end; var now = new Date(); // Get current day name var currentDayName = dUtil.getDayName(); // Get activities from array for (var i=0, iLen=data.length; i&lt;iLen; i++) { if (data[i].day == currentDayName) { todayActivities = data[i].activities; break; } } // If there are no activities for today, return undefined if (!todayActivities) return; // For each activity today, see what are on now for (var j=0, jLen=todayActivities.length; j&lt;jLen; j++) { activity = todayActivities[j]; name = activity.activity; // Loop over times to see if on now times = activity.times; for (var k=0, kLen=times.length; k&lt;kLen; k++) { if (dUtil.timeToDate(times[k].start) &lt; now &amp;&amp; dUtil.timeToDate(times[k].end) &gt; now) { // Add activity to result array // Format to whatever is needed // This gives an array of strings like "Freestyle - 6:45 PM to 7:30 PM" currentActivities.push(name + ' - ' + times[k].start + ' to ' + times[k].end); } } } return currentActivities; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload