-1

Let's say that i have a JSON file like this

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

And current time is (Apr 05 2017 12:30:00 GMT+0200 (EET)) how can i get the current program from those time ranges using Javascript also i can use library like momentJS

dyaa
  • 1,440
  • 18
  • 43

2 Answers2

1

You can do something like this:

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);
Titus
  • 22,031
  • 1
  • 23
  • 33
  • Though I don't have right, I'd like to request you to delete the answer as OP is yet to show code. Even I have an answer ready, but lets wait till OP shows his code. – Rajesh Apr 05 '17 at 11:12
  • @Rajesh i don't have a ready code, I posted a question Titus posted the answer and it worked :) ... – dyaa Apr 05 '17 at 11:21
  • @dyaa the reason i requested Titus to remove answer and you to post your code is because that way we could help you with more than just answer. Now you have a working code but do you know, how it works? It's pros and cons? Trying on your own would hello you learn – Rajesh Apr 05 '17 at 11:24
1

Note that the solution given by Tisus may not work on every browser since date parsing of non-standard string is system-dependent.

I suggest to parse your input using moment(String, String) instead of new Date(String). Then you can use moment isBetween to check if the current day is in the given range.

Here a live sample:

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>

As side note: I agree with Rajesh, providing code attempt would help getting better answers and gives you the chance to learn better.

VincenzoC
  • 30,117
  • 12
  • 90
  • 112
  • prog is undefined – dyaa Apr 05 '17 at 15:20
  • You are getting `undefined` because current time is after `2017-04-05 16:00:00` that is the lastest time in your input. I've edited my snippet mocking current time to always get the same result. – VincenzoC Apr 05 '17 at 15:31