如何从时间范围获取当前程序

How to get current program from time ranges

假设我有一个这样的 JSON 文件

[
    {
        "programId": "bla-bla-1",
        "programTitle": "bla bla 1",
        "programImg": "bla1.jpg",
        "startsAt": "Apr 05 2017 11:00:00 GMT+0200 (EET)",
        "endsAt": "Apr 05 2017 12:00:00 GMT+0200 (EET)"
    },
    {
        "programId": "bla-bla-2",
        "programTitle": "bla bla 2",
        "programImg": "bla2.jpg",
        "startsAt": "Apr 05 2017 12:00:00 GMT+0200 (EET)",
        "endsAt": "Apr 05 2017 14:00:00 GMT+0200 (EET)"
    },
    {
        "programId": "bla-bla-3",
        "programTitle": "bla bla 3",
        "programImg": "bla3.jpg",
        "startsAt": "Apr 05 2017 14:00:00 GMT+0200 (EET)",
        "endsAt": "Apr 05 2017 16:00:00 GMT+0200 (EET)"
    }
]

当前时间是(2017 年 4 月 5 日 12:30:00 GMT+0200 (EET))我怎样才能得到使用 Javascript 的那些时间范围内的当前程序我也可以使用像 momentJS

这样的库

你可以这样做:

var obj = [ { "programId": "bla-bla-1", "programTitle": "bla bla 1", "programImg": "bla1.jpg", "startsAt": "Apr 05 2017 11:00:00 GMT+0200 (EET)", "endsAt": "Apr 05 2017 12:00:00 GMT+0200 (EET)" }, { "programId": "bla-bla-2", "programTitle": "bla bla 2", "programImg": "bla2.jpg", "startsAt": "Apr 05 2017 12:00:00 GMT+0200 (EET)", "endsAt": "Apr 05 2017 14:00:00 GMT+0200 (EET)" }, {"programId": "bla-bla-3", "programTitle": "bla bla 3", "programImg": "bla3.jpg", "startsAt": "Apr 05 2017 14:00:00 GMT+0200 (EET)", "endsAt": "Apr 05 2017 16:00:00 GMT+0200 (EET)"}];

var now = new Date("Apr 05 2017 12:30:00 GMT+0200 (EET)").getTime();//new Date().getTime();

var prog = obj.find(o => {
  return (now >= new Date(o.startsAt).getTime() && now <= new Date(o.endsAt).getTime());
});

console.log(prog);

请注意,Tisus 给出的解决方案可能不适用于所有浏览器,因为非标准字符串的日期解析是系统相关的。

我建议使用 moment(String, String) instead of new Date(String). Then you can use moment isBetween 解析您的输入,以检查当前日期是否在给定范围内。

这是一个活生生的例子:

var input = [
    {
        "programId": "bla-bla-1",
        "programTitle": "bla bla 1",
        "programImg": "bla1.jpg",
        "startsAt": "Apr 05 2017 11:00:00 GMT+0200 (EET)",
        "endsAt": "Apr 05 2017 12:00:00 GMT+0200 (EET)"
    },
    {
        "programId": "bla-bla-2",
        "programTitle": "bla bla 2",
        "programImg": "bla2.jpg",
        "startsAt": "Apr 05 2017 12:00:00 GMT+0200 (EET)",
        "endsAt": "Apr 05 2017 14:00:00 GMT+0200 (EET)"
    },
    {
        "programId": "bla-bla-3",
        "programTitle": "bla bla 3",
        "programImg": "bla3.jpg",
        "startsAt": "Apr 05 2017 14:00:00 GMT+0200 (EET)",
        "endsAt": "Apr 05 2017 16:00:00 GMT+0200 (EET)"
    }
]

var prog = input.find(item => {
  //var now = moment();
  // mocking current time in order to get always the same result
  var now = moment("Apr 05 2017 12:30:00 GMT+0200 (EET)", 'MMM DD YYYY HH:mm:ss Z')
  var start = moment(item.startsAt, 'MMM DD YYYY HH:mm:ss Z');
  var end = moment(item.endsAt, 'MMM DD YYYY HH:mm:ss Z');
  return now.isBetween(start, end);
});

console.log(prog);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>


旁注:我同意 Rajesh 的观点,提供代码尝试将有助于获得更好的答案并让您有机会更好地学习。