-1

假设我有一个这样的 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 这样的库

4

2 回答 2

1

你可以这样做:

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

于 2017-04-05T11:09:06.933 回答
1

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

我建议使用moment(String, String)而不是解析您的输入new Date(String)。然后您可以使用 momentisBetween检查当前日期是否在给定范围内。

这是一个现场样本:

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 的观点,提供代码尝试将有助于获得更好的答案,并让您有机会更好地学习。

于 2017-04-05T14:55:33.760 回答