1

I have defined a set of rules.

  var rules = [
    {
    time :  "12:00:00 pm",
    time1 :  "01:00:00 pm",
    value:0
    },
   {
    time :  "01:00:00 pm",
    time1 :  "02:00:00 pm",
    value:0
    }, 
    {
    time :  "02:00:00 pm",
        time1 :  "03:00:00 pm",
        value:0
    }, 
    {
    time :  "03:00:00 pm",
        time1 : "04:00:00 pm",
        value:0
    }, {
    time : "04:00:00 pm",
        time1 : "05:00:00 pm",
        value:0
    }
]

module.exports = rules;

Now I have got an array of data which consists of time I want to compare the dats with these rules and accordingly increment the value of status in the rules array. I am using moment.Js to compare but its isAfter() and isBefore functions are not working for me as they should.

  _.each(data, function(data){
            // this comes from database where data.start_time consists of time

            console.log(data.start_time); // Mon Oct 03 2016 12:40:36 GMT+0530 (India Standard Time)


            var d = moment(data.start_time).format("hh:mm:ss a");
            console.log(d); //12:40:36 pm

            _.each(rules, function(rule){
               console.log(rule.time); //12:00:00 pm

             if(moment(d).isAfter(rule.time))
             {
                // Not able to reach here tried both isAfter
                // isBefore everything.
                console.log("sasa");
             }
         })
       });

Also if there is any faster or better way to do these kind of comparision please can you leave a gist below. I will check that out too.

Deepak Singh
  • 35
  • 1
  • 3

2 Answers2

2

I tried to debug your code and have some observations -

  1. moment(d).isAfter - here, moment(d) does not have a method called "isAfter". This is a strange thing as it should have this method as per the documentation. I had to call the prototype method like below -

    var protoType = moment(start_time).__proto__;
    var isafter = protoType.isAfter(rule.time);
    

But, this led me to another issue - Maximum call stack size exceeded (Stack Overflowed ;p).

  1. Also, when I don't format the date in the way you are formatting (before calling the isAfter method), it works fine. Though I had a dummy value for rule.time variable. Moreover, I had to keep both the dates in the same format.

So, based on the observations, I would suggest you to not rip off the date part from the date by formatting it before comparing, since I guess it is required.

gkb
  • 1,449
  • 2
  • 15
  • 30
  • take a look at this http://stackoverflow.com/questions/22600856/moment-js-date-time-comparison – gkb Oct 19 '16 at 12:39
0

moment().isAfter() as well as moment().isBefore() methods will take input in the following format.

    YYYY-MM-DD Or YYYY-MM-DD hh:mm:ss

In your case its in hh:mm:ss format, because of which its returning false everytime. if u want to use this methods then changes your rules array as follows

var rules = [
    {
        time: "2016-10-03 12:00:00 pm",
        time1: "2016-10-03 01:00:00 pm",
        value: 0
    },
    {
        time: "2016-10-03 01:00:00 pm",
        time1: "2016-10-03 02:00:00 pm",
        value: 0
    }
]

And format the data.start_time as follows

    var d = moment(data.start_time).format("YYYY-MM-DD hh:mm:ss a");

This will give you required result.

kgangadhar
  • 4,886
  • 5
  • 36
  • 54