0

I am using Moment.js to compare dates, but I get something odd. My code:

            var extraStackData = function (data, from_date1, end_date1) {
                var result = {};
                for (var i in data) {
                    var row = data[i];
                    if (typeof result[row['know_source']] == 'undefined') {
                        result[row['know_source']] = {};
                    }
                    result[row['know_source']][row['create_date']] = parseInt(row['sum']);
                }
//                console.log(result);
                console.log(from_date1);
                console.log(end_date1);
                console.log(from_date1 > end_date1);
                var cur_date = from_date1;
                console.log(cur_date);
                console.log(cur_date.isAfter(end_date1));
                for (var source in result) {
                    for (var cur_date = from_date; cur_date.isBefore(end_date); cur_date.add("days", 1)) {
                        console.log(cur_date);
                        if (typeof result[source][cur_date] == 'undefined') {
                            result[source][cur_date] = 0;
                        }
                    }
//                    console.log(result[source])
                }

The result is as follows:

Run result

According to the output, it seems that the variable from_date='2014-10-1' is larger than end_date='2014-11-18'. Can someone help me out?

Update: I have found an even weirder thing. The following piece of code:

                console.log(from_date);
                console.log(end_date);
                var days = from_date.diff(end_date, 'days');
                console.log(days);

in which I use diff to get the interval days, gives the following output:

enter image description here

Baumannzone
  • 760
  • 2
  • 19
  • 38
ssj
  • 1,737
  • 2
  • 16
  • 28
  • 2
    does the arguments **from_date1** and **end_date1** are moment object? – SG_ Nov 18 '14 at 08:12
  • Looks like you modify the object after having it logged. – Bergi Nov 18 '14 at 08:17
  • I think I just find where the problem is. I pass the from_date and to_date to another function which is modified by that.`function dateList(from_date, to_date) { var dates = []; var cur_date = to_date; while (!cur_date.isBefore(from_date)) { dates.push(cur_date.format('YYYY-MM-DD')); cur_date.subtract('days', 1); } return dates; }` – ssj Nov 18 '14 at 08:29
  • 1
    What you see in the output [is not the state the objects had when they were logged](http://stackoverflow.com/questions/23392111/console-log-async-or-sync). – Bergi Nov 18 '14 at 09:09

2 Answers2

0

I have the same problem that my moment.js is in version 2.12.0
It seems like the problem was caused by the format method :

moment(....).format("YYYY-MM-DD");

I use the format "YYYY-M-D" which is close to yours , then my
result of comparison presents that earlier time was larger than later
time . After I remove the format method when I initial the moment object ,
comparison works correctly .

Carr
  • 2,691
  • 1
  • 19
  • 27
0

If output comes wrong, then just use moment function without converting to a specific format.

   moment('your_date').format();