JavaScript与数组的时间比较

时间:2017-04-27 09:12:50

标签: javascript arrays json time compare

我正在开展一个项目,我需要获得当天所有空置的教室,并基本上过滤掉正在使用的教室。

我从学校自己的API获取所有数据,其中JSON响应正文如下所示:

{
   "status": "success",
   "reservations": [
      {
         "id": "18935",
         "subject": "subjectName",
         "modifiedDate": "2017-04-27T06:04:51",
         "startDate": "2017-04-27T11:00:00",
         "endDate": "2017-04-27T13:00:00",
         "resources": [
            {
               "id": "50",
               "type": "room",
               "code": "A440.5",
               "parent": {
                  "id": "2",
                  "type": "building",
                  "code": "A",
                  "name": "buildingA"
               },
               "name": "A440.5"
            },
            {
               "id": "2995",
               "type": "realization",
               "code": "",
               "name": ""
            },
            {
               "id": "2267",
               "type": "student_group",
               "code": "",
               "name": ""
            }
         ],
         "description": ""
      },
      {
         "id": "20362",
         "subject": "subjectName",
         "modifiedDate": "2017-04-27T06:05:05",
         "startDate": "2017-04-27T11:00:00",
         "endDate": "2017-04-27T14:00:00",
         "resources": [
            {
               "id": "51",
               "type": "room",
               "code": "A450.1",
               "parent": {
                  "id": "2",
                  "type": "building",
                  "code": "A",
                  "name": "buildingA"
               },
               "name": "A450.1"
            },
            {
               "id": "2402",
               "type": "student_group",
               "code": "",
               "name": ""
            },
            {
               "id": "3064",
               "type": "realization",
               "code": "",
               "name": ""
            }
         ],
         "description": ""
      },
      {
         "id": "20237",
         "subject": "subjectName",
         "modifiedDate": "2017-04-27T06:05:05",
         "startDate": "2017-04-27T11:15:00",
         "endDate": "2017-04-27T13:00:00",
         "resources": [
            {
               "id": "45",
               "type": "room",
               "code": "A420.4",
               "parent": {
                  "id": "2",
                  "type": "building",
                  "code": "A",
                  "name": "buildingA"
               },
               "name": "A420.4"
            },
            {
               "id": "2433",
               "type": "student_group",
               "code": "",
               "name": ""
            },
            {
               "id": "3058",
               "type": "realization",
               "code": "",
               "name": ""
            }
         ],
         "description": ""
      },
      {
         "id": "20888",
         "subject": "subjectName",
         "modifiedDate": "2017-04-27T06:04:57",
         "startDate": "2017-04-27T13:15:00",
         "endDate": "2017-04-27T16:00:00",
         "resources": [
            {
               "id": "62",
               "type": "room",
               "code": "A520.5",
               "parent": {
                  "id": "2",
                  "type": "building",
                  "code": "A",
                  "name": "buildingA"
               },
               "name": "A520.5"
            },
            {
               "id": "3092",
               "type": "realization",
               "code": "",
               "name": ""
            },
            {
               "id": "2444",
               "type": "student_group",
               "code": "",
               "name": ""
            }
         ],
         "description": ""
      },
      {
         "id": "22586",
         "subject": "subjectName",
         "modifiedDate": "2017-04-27T06:04:48",
         "startDate": "2017-04-27T13:15:00",
         "endDate": "2017-04-27T17:00:00",
         "resources": [
            {
               "id": "52",
               "type": "room",
               "code": "A450.3",
               "parent": {
                  "id": "2",
                  "type": "building",
                  "code": "A",
                  "name": "buildingA"
               },
               "name": "A450.3"
            },
            {
               "id": "3004",
               "type": "realization",
               "code": "",
               "name": ""
            },
            {
               "id": "2294",
               "type": "student_group",
               "code": "",
               "name": ""
            },
            {
               "id": "525",
               "type": "student_group",
               "code": "",
               "name": ""
            }
         ],
         "description": ""
      },
      {
         "id": "18816",
         "subject": "subjectName",
         "modifiedDate": "2017-04-27T06:04:58",
         "startDate": "2017-04-27T13:15:00",
         "endDate": "2017-04-27T16:00:00",
         "resources": [
            {
               "id": "41",
               "type": "room",
               "code": "A340.1",
               "parent": {
                  "id": "2",
                  "type": "building",
                  "code": "A",
                  "name": "buildingA"
               },
               "name": "A340.1"
            },
            {
               "id": "2989",
               "type": "realization",
               "code": "",
               "name": ""
            },
            {
               "id": "795",
               "type": "student_group",
               "code": "",
               "name": ""
            },
            {
               "id": "599",
               "type": "student_group",
               "code": "",
               "name": ""
            }
         ],
         "description": ""
      },
      {
         "id": "20431",
         "subject": "subjectName",
         "modifiedDate": "2017-04-27T06:04:56",
         "startDate": "2017-04-27T13:15:00",
         "endDate": "2017-04-27T16:00:00",
         "resources": [
            {
               "id": "40",
               "type": "room",
               "code": "A320.7",
               "parent": {
                  "id": "2",
                  "type": "building",
                  "code": "A",
                  "name": "buildingA"
               },
               "name": "A320.7/8"
            },
            {
               "id": "2416",
               "type": "realization",
               "code": "",
               "name": ""
            },
            {
               "id": "2386",
               "type": "student_group",
               "code": "",
               "name": ""
            }
         ],
         "description": ""
      },
      {
         "id": "18588",
         "subject": "subjectName",
         "modifiedDate": "2017-04-27T06:04:49",
         "startDate": "2017-04-27T13:15:00",
         "endDate": "2017-04-27T16:00:00",
         "resources": [
            {
               "id": "25",
               "type": "room",
               "code": "A130.1",
               "parent": {
                  "id": "2",
                  "type": "building",
                  "code": "A",
                  "name": "buildingA"
               },
               "name": "A130.1"
            },
            {
               "id": "26",
               "type": "room",
               "code": "A130.3",
               "parent": {
                  "id": "2",
                  "type": "building",
                  "code": "A",
                  "name": "buildingA"
               },
               "name": "A130.3"
            },
            {
               "id": "2979",
               "type": "realization",
               "code": "",
               "name": ""
            },
            {
               "id": "582",
               "type": "student_group",
               "code": "",
               "name": ""
            }
         ],
         "description": ""
      },      
      {
         "id": "18940",
         "subject": "subjectName",
         "modifiedDate": "2017-04-27T06:04:53",
         "startDate": "2017-04-27T13:15:00",
         "endDate": "2017-04-27T16:00:00",
         "resources": [
            {
               "id": "2996",
               "type": "realization",
               "code": "",
               "name": ""
            },
            {
               "id": "2267",
               "type": "student_group",
               "code": "",
               "name": ""
            },
            {
               "id": "2268",
               "type": "student_group",
               "code": "",
               "name": ""
            },
            {
               "id": "31",
               "type": "room",
               "code": "A210.2",
               "parent": {
                  "id": "2",
                  "type": "building",
                  "code": "A",
                  "name": "buildingA"
               },
               "name": "A210.2"
            }
         ],
         "description": ""
      },
      {
         "id": "12041",
         "subject": "subjectName",
         "modifiedDate": "2017-04-27T06:04:53",
         "startDate": "2017-04-27T14:15:00",
         "endDate": "2017-04-27T17:00:00",
         "resources": [
            {
               "id": "2510",
               "type": "realization",
               "code": "",
               "name": ""
            },
            {
               "id": "775",
               "type": "student_group",
               "code": "",
               "name": ""
            },
            {
               "id": "23",
               "type": "room",
               "code": "A520.7",
               "parent": {
                  "id": "2",
                  "type": "building",
                  "code": "A",
                  "name": "buildingA"
               },
               "name": "A520.7"
            }
         ],
         "description": ""
      },
      {
         "id": "24630",
         "subject": "subjectName",
         "modifiedDate": "2017-04-27T06:05:05",
         "startDate": "2017-04-27T14:15:00",
         "endDate": "2017-04-27T17:00:00",
         "resources": [
            {
               "id": "3277",
               "type": "realization",
               "code": "",
               "name": ""
            },
            {
               "id": "42",
               "type": "room",
               "code": "A340.2",
               "parent": {
                  "id": "2",
                  "type": "building",
                  "code": "A",
                  "name": "buildingA"
               },
               "name": "A340.2"
            }
         ],
         "description": ""
      },
      {
         "id": "27205",
         "subject": "subjectName",
         "modifiedDate": "2017-04-27T06:05:07",
         "startDate": "2017-04-27T14:15:00",
         "endDate": "2017-04-27T17:00:00",
         "resources": [
            {
               "id": "35",
               "type": "room",
               "code": "A240.2",
               "parent": {
                  "id": "2",
                  "type": "building",
                  "code": "A",
                  "name": "buildingA"
               },
               "name": "A240.2"
            },
            {
               "id": "775",
               "type": "student_group",
               "code": "",
               "name": ""
            },
            {
               "id": "3384",
               "type": "realization",
               "code": "",
               "name": ""
            }
         ],
         "description": ""
      },
      {
         "id": "25917",
         "subject": "subjectName",
         "modifiedDate": "2017-04-27T06:05:00",
         "startDate": "2017-04-27T15:15:00",
         "endDate": "2017-04-27T16:00:00",
         "resources": [
            {
               "id": "36",
               "type": "room",
               "code": "A240.4",
               "parent": {
                  "id": "2",
                  "type": "building",
                  "code": "A",
                  "name": "buildingA"
               },
               "name": "A240.4"
            },
            {
               "id": "593",
               "type": "realization",
               "code": "",
               "name": ""
            },
            {
               "id": "595",
               "type": "student_group",
               "code": "",
               "name": ""
            }
         ],
         "description": ""
      },
      {
         "id": "21932",
         "subject": "subjectName",
         "modifiedDate": "2017-04-27T06:05:06",
         "startDate": "2017-04-27T16:00:00",
         "endDate": "2017-04-27T18:00:00",
         "resources": [
            {
               "id": "43",
               "type": "room",
               "code": "A350.1",
               "parent": {
                  "id": "2",
                  "type": "building",
                  "code": "A",
                  "name": "buildingA"
               },
               "name": "A350.1"
            },
            {
               "id": "2464",
               "type": "student_group",
               "code": "",
               "name": ""
            },
            {
               "id": "2747",
               "type": "student_group",
               "code": "",
               "name": ""
            }
         ],
         "description": ""
      }
   ]
}

为了获得JSON响应,我需要放置startDate(当用户搜索空房时,在这种情况下它是:2017-04-27T10:55)和endDate(设置为结束)当天,2017-04-27T22:00)。此查询的结果是上面的JSON响应。

问题在于,我使用的API并不包含空房的任何数据,但仅限于预定某些时间的数据,因此我列出了建筑物中的所有房间并进行了比较它被预定的过滤掉了:

var rooms = ['A120.3', 'A130.1', 'A130.3', 'A140.1', 'A140.2', 'A140.4', 'A250.1', 'A240.4', 'A240.2', 'A220.5', 'A220.3',
'A220.1', 'A210.2', 'A320.2', 'A320.6', 'A320.7', 'A320.8', 'A340.1', 'A340.2', 'A350.1', 'A350.3', 'A440.5', 'A450.3','A450.1', 
'A440.4', 'A440.2', 'A420.6', 'A420.5', 'A420.4', 'A420.2', 'A510.2', 'A520.5', 'A510.4', 'A520.6', 'A520.7','A540.1', 'A540.2'];


var data = JSON.parse(responseText);
var booking = Object.create(null);
var free;

data.reservations.forEach(function (reservation) {
    reservation.resources.some(function (resource) {
        if (resource.type === 'room') {
            booking[resource.code] = booking[resource.code] || [];
            booking[resource.code].push({ startDate: reservation.startDate, endDate: reservation.endDate });
            return true;
        }
    });
});

free = rooms.filter(function (a) {
    return !booking[a];
});

console.log(free);

结果:

A210.3
A220.5
A320.2
A520.6
A510.4

这只返回那些白天根本没有使用的那些,但我需要把那些闲置一小时或两小时。

对于此预订课程,例如:

"startDate": "2017-04-27T13:15:00",
"endDate": "2017-04-27T16:00:00",

"type": "room",
"code": "A520.5"

为此,我需要打印出来:

A520.5 - 2 hours 20 minutes

所以我需要从搜索的startDate 2017-04-27T10:55 )获取房间/时间,并将其与startDate的{​​{1}}进行比较预订房间( 2017-04-27T13:15:00 )以获得该房间空置的剩余时间。

要澄清:

我拥有从 2017-04-27T10:55 开始预订房间的所有数据 直到 2017-04-27T22:00 ,您可以从上面的JSON响应中看到。

我需要以某种方式比较用户时JSON查询的startDate 搜索房间( 2017-04-27T10:55 )并将其与预订进行比较 房间startDate以获得房间空置的时间。

这就是我为JSON查询执行startDateendDate的方式:

// Timestamp needs to be formed (YYYY-MM-DDTxx:xx) in order for JSON query to work

 var todaysDate = new Date();

            function convertDate(date) {
                var yyyy = date.getFullYear().toString();
                var mm = (date.getMonth() + 1).toString();
                var dd = date.getDate().toString();


                var mmChars = mm.split('');
                var ddChars = dd.split('');

                return yyyy + '-' + (mmChars[1] ? mm : "0" + mmChars[0]) + '-' + (ddChars[1] ? dd : "0" + ddChars[0]);
            }

            // Current time when user searches for vacant rooms
            var currentTime = new Date();
            var time = "T" + currentTime.getHours() + ":" + currentTime.getMinutes();

// variables for the JSON query
var startDate = convertDate(todaysDate) + time;
var endDate = convertDate(todaysDate) + ("T22:00");

// JSON-query
            var getRooms = {
                "startDate": startDate,
                "endDate": endDate
            };

0 个答案:

没有答案