1

I have a JSON with a list of item. The JSON have current time. Each menu item have start time and end time. I want to display the items depends on the time. Is it possible? I have created a fiddle for this. FIDDLE

Please see the below JSON

{"time":"Thu Mar 23 4:10:44 -0700 2017","breakfast":{"start_time":"Thu Mar 23 1:00:00 -0700 2017","end_time":"Thu Mar 23 3:00:00 -0700 2017","soup":[{"id":"b_01","name":"BF_one"},{"id":"b_02","name":"BF_two"}]},"lunch":{"start_time":"Thu Mar 23 4:00:00 -0700 2017","end_time":"Thu Mar 23 6:00:00 -0700 2017","salad":[{"id":"l_01","name":"L_one"},{"id":"l_02","name":"L_two"}]},"dinner":{"start_time":"Thu Mar 23 7:00:00 -0700 2017","end_time":"Thu Mar 23 9:00:00 -0700 2017","meal":[{"id":"m_01","name":"M_one"},{"id":"m_02","name":"M_two"}]}}

Please help.

1 Answer 1

1

You can take the differences in date

For example your current time in JSON is

Thu Mar 23 4:10:44 -0700 2017

Setting this as your current time

var currenttime = new Date($scope.data["time"]);

Now you can loop through each property in your object and compare start time and end time with current time

for (var key in $scope.data) {
        if (key !== "time") {
        var starttime = new Date($scope.data[key]["start_time"]);
        var endtime = new Date($scope.data[key]["end_time"]);
        var startdiff = (currenttime - starttime);
        var enddiff = (currenttime - endtime);
            if(startdiff < 0 && enddiff < 0){

          $scope.text = ("Next activity \""+key+"\" in "+Math.abs(Math.round(((startdiff % 86400000) % 3600000) / 60000))+"min");
          $scope.activity="Next";
          break;
        }
        else if(startdiff > 0 && enddiff > 0)
        {
            //Activity has finished
        }
        else{
            $scope.text = ("On going "+key);
          $scope.activity = key;
          break;
        }
        }
        } 

if startdiff > 0 && enddiff > 0 condition is true --> Activity has finished

if startdiff < 0 && enddiff < 0 condition is true --> You are between previous and next activity

else --> You have an on going activity

The line

Math.round(((startdiff % 86400000) % 3600000) / 60000)

will give you difference in minutes

As this will always be a -ve value surround it in Math.abs(...)

Here is the FULL EXAMPLE

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.