Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8961571
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:55:29+00:00 2026-06-15T15:55:29+00:00

I am using JavaScript, JSON, and date.js and I’m trying to parse the JSON

  • 0

I am using JavaScript, JSON, and date.js and I’m trying to parse the JSON file to display only the list of classes that are taking place at the current time. I’ve been able to parse the JSON file so it only shows classes that take place on the current day. I haven’t been able to parse the times correctly though.

Part of my JSON file:

[
{
    "day": "Monday",
    "activities": [
        {
            "activity": "Freestyle ",
            "times": [
                {"start":"6:30 AM","end":"7:15 AM"},
                {"start":"7:15 AM","end":"8:15 AM"},
                {"start":"5:15 PM","end":"6:00 PM"}
            ]
        },
        {
            "activity": "Open Skate ",
            "times": [
                {"start":"11:30 AM","end":"5:00 PM"},
                {"start":"7:30 PM","end":"9:30 PM"}
            ]
        },
        {
            "activity": "Holiday Snowbunnies 1 ",
            "times": [
                {"start":"6:00 PM","end":"6:30 PM"}
            ]
        }
    ]
},
{
    "day": "Tuesday",
    "activities": [
        {
            "activity": "Freestyle ",
            "times": [
                {"start":"5:45 AM","end":"6:30 AM"},
                {"start":"6:30 AM","end":"7:15 AM"},
                {"start":"6:45 PM","end":"7:30 PM"}
            ]
        },
        {
            "activity": "Open Skate ",
            "times": [
                {"start":"10:00 AM","end":"5:00 PM"},
                {"start":"7:30 PM","end":"9:30 PM"}
            ]
        },
        {
            "activity": "Patch ",
            "times": [
                {"start":"10:00 AM","end":"10:30 AM"}
            ]
        }
    ]
}
]

My JavaScript – With Appropriate Function:

function updateSchedule(responseText){
var activitiesDiv = document.getElementById("activities");
var skActivities = JSON.parse(responseText);
var toDay = Date.today().getDayName();
var now = new Date();
var curHour = now.toTimeString('hh:mm tt');

for(var i=0;i<skActivities.length;i++){
    var activityDay = skActivities[i];
    if(activityDay.day == toDay)
    {   
        var toDaysActivities = activityDay.activities;
        for(var j=0;j<toDaysActivities.length;j++){
            var toDayActivity = toDaysActivities[j];

            var curTimeDayActivities = toDayActivity.times;
            for(var k=0;k<curTimeDayActivities.length;k++){
                var curTimeDayStartActivity = curTimeDayActivities[k].start;

                if(curTimeDayStartActivity >= curHour){
                    var div = document.createElement("div");
                    div.innerHTML = toDayActivity.activity + ": " + curTimeDayActivities[k].start;
                    activitiesDiv.appendChild(div);
                }
            }
        }
    }
    continue;
  }
}

Currently the function is returning all classes and their times that take place on the current day of the week. NOTE: The exception is the “11:30 AM Open Skate”. It seems like the only values returned are those that are 9 and under.

Again, my intent is for a user to visit the site and see the classes that are currently taking place.

NOTE: If we don’t need the solution to utilize date.js, I’m fine with that. Just thought I would incorporate it.

Any recommendations would be greatly appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T15:55:30+00:00Added an answer on June 15, 2026 at 3:55 pm

    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.

    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).

    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<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<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<kLen; k++) {
    
          if (dUtil.timeToDate(times[k].start) < now && 
              dUtil.timeToDate(times[k].end) > 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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to edit a JSON file using javaScript? I'm trying to read
I am using the Javascript serializer to parse a JSON file. Its runs fine
I'm using .NET to generate a JSON file that has many Date s in
I parse data using JSON in javascript, I find this is very convenient. But
I have a working JavaScript code below which dynamically creates JSON object using JSON.parse
I would like to parse the external JSON file in javascript which has an
I am trying to set a javascript date so that it can be submitted
I'm trying to display a PHP variable using javascript/jquery but it's displaying 'null'. if(mysql_num_rows($checkBan)
I am receiving date in json response and parsing it using date-format javascript library.
I cant find one way to get this value (comment) into json using javascript.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.