如何比较给定时间,落在特定时间范围内 - javascript

时间:2018-03-08 19:18:24

标签: javascript node.js

我正在使用节点js为时尚相关应用程序创建后端,我需要根据当前时间显示特定商店是打开还是关闭。那么,我如何将当前时间与商店的开始结束时间进行比较?任何帮助,将不胜感激。存储在db中的计时数据集如下:

"timings" : [
    {
      "id": 1,
      "day": "Monday",
      "session_one": {
        "start_time": "10:00 AM",
        "end_time": "02:00 PM"
      },
      "session_two": {
        "start_time": "04:00 PM",
        "end_time": "09:00 PM"
      },
      "session_three": {
        "start_time": "03:00 PM",
        "end_time": "07:30 PM"
      }
    },
    {
      "id": 2,
      "day": "Tuesday",
      "session_one": {
        "start_time": "10:00 AM",
        "end_time": "02:00 PM"
      },
      "session_two": {
        "start_time": "04:00 PM",
        "end_time": "09:00 PM"
      },
      "session_three": {
        "start_time": "03:00 PM",
        "end_time": "07:30 PM"
      }
    },
    {
      "id": 3,
      "day": "Wednesday",
      "session_one": {
        "start_time": "10:00 AM",
        "end_time": "02:00 PM"
      },
      "session_two": {
        "start_time": "04:00 PM",
        "end_time": "09:00 PM"
      },
      "session_three": {
        "start_time": "03:00 PM",
        "end_time": "07:30 PM"
      }
    },
    {
      "id": 4,
      "day": "Thursday",
      "session_one": {
        "start_time": "10:00 AM",
        "end_time": "02:00 PM"
      },
      "session_two": {
        "start_time": "04:00 PM",
        "end_time": "09:00 PM"
      },
      "session_three": {
        "start_time": "03:00 PM",
        "end_time": "07:30 PM"
      }
    },
    {
      "id": 5,
      "day": "Friday",
      "session_one": {
        "start_time": "10:00 AM",
        "end_time": "02:00 PM"
      },
      "session_two": {
        "start_time": "04:00 PM",
        "end_time": "09:00 PM"
      },
      "session_three": {
        "start_time": "03:00 PM",
        "end_time": "07:30 PM"
      }
    },
    {
      "id": 6,
      "day": "Saturday",
      "session_one": {
        "start_time": "10:00 AM",
        "end_time": "02:00 PM"
      },
      "session_two": {
        "start_time": "04:00 PM",
        "end_time": "09:00 PM"
      },
      "session_three": {
        "start_time": "03:00 PM",
        "end_time": "07:30 PM"
      }
    },
    {
      "id": 7,
      "day": "Sunday",
      "session_one": {
        "start_time": "10:00 AM",
        "end_time": "02:00 PM"
      },
      "session_two": {
        "start_time": "04:00 PM",
        "end_time": "09:00 PM"
      },
      "session_three": {
        "start_time": "03:00 PM",
        "end_time": "07:30 PM"
      }
    }
  ]

1 个答案:

答案 0 :(得分:1)

试试这个,

function timeCal(time) {
    let timeSplit = time.split(' ');
    let timeInMinutes = 0;

    if(timeSplit[1] === 'AM') {
        let timeArr = timeSplit[0].split(':');

        let hours = parseInt(timeArr[0]);
        let minutes = parseInt(timeArr[1]); 

        timeInMinutes = (hours === 12) ? minutes : (hours * 60) + minutes;

    } else if(timeSplit[1] === 'PM') {
        let timeArr = timeSplit[0].split(':');

        let hours = parseInt(timeArr[0]);
        let minutes = parseInt(timeArr[1]); 

        timeInMinutes = (hours === 12) ? (hours * 60 + minutes) : ((hours + 12) * 60) + minutes;
    }

    return timeInMinutes;
}


let time =  {
        "start_time": "10:20 AM",
        "end_time": "02:00 PM"
      };

let start = timeCal(time.start_time);
let end = timeCal(time.end_time);
let today = new Date();
let current = today.getHours() * 60 + today.getMinutes();

(start<=current && end>=current) ? console.log('open') : console.log('closed');