如何使用date.js将JSON中的时间对象与另一时间进行比较

时间:2012-12-06 04:24:12

标签: javascript json datejs

我正在使用JavaScript,JSON和date.js,我正在尝试解析JSON文件,以便仅显示当前时间发生的类列表。我已经能够解析JSON文件,因此它只显示当天发生的类。我虽然无法正确解析时间。

我的JSON文件的一部分:

[
{
    "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"}
            ]
        }
    ]
}
]

我的JavaScript - 使用适当的功能:

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;
  }
}

目前,该函数返回所有类及其在一周中当天发生的时间。 注意:例外情况是“11:30 AM Open Skate”。看起来返回的唯一值是那些9及以下的值。

同样,我的目的是让用户访问该网站并查看当前正在进行的课程。

注意:如果我们不需要使用date.js的解决方案,那我很好。我以为我会加入它。

任何建议都将不胜感激。

1 个答案:

答案 0 :(得分:1)

我认为日期库在这里不会有太多帮助,它只会添加膨胀和其他东西来维护和担心。以下内容获取“当前”活动,由您决定格式化返回的数据并对其执行某些操作。

它不需要太多的日期支持,它主要是过滤活动。将时间转换为日期对象进行比较是微不足道的(3行代码)。

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;
}