Rakesh K
Rakesh K

Reputation: 51

Confused looping the dates in moment js

I'm writing a small piece of javascript that will help me get the values between 2 dates (including today).

Here is my code.

var beforeSevenDays = moment(Date.now() - 7 * 24 * 3600 * 1000).format('YYYY-MM-DD');
var i = 0;
for (var m = moment(beforeSevenDays); m.diff(moment(Date.now()).local('in'), 'days') <= 0; m.add(1, 'days')) {
    console.log("i: " + i + " " + m.format('YYYY-MM-DD'));
    i += 1;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>

when I run this code, the output that I get is

i: 0 2018-10-03
i: 1 2018-10-04
i: 2 2018-10-05
i: 3 2018-10-06
i: 4 2018-10-07
i: 5 2018-10-08
i: 6 2018-10-09
i: 7 2018-10-10
i: 8 2018-10-11

but the output that I'm expecting is

i: 0 2018-10-03
i: 1 2018-10-04
i: 2 2018-10-05
i: 3 2018-10-06
i: 4 2018-10-07
i: 5 2018-10-08
i: 6 2018-10-09
i: 7 2018-10-10

here when I modify the query as m.diff(moment(Date.now()).local('in'), 'days') < 0 I get the output as

i: 0 2018-10-03
i: 1 2018-10-04
i: 2 2018-10-05
i: 3 2018-10-06
i: 4 2018-10-07
i: 5 2018-10-08
i: 6 2018-10-09

please let me know where I'm going wrong and how can I fix it.

Upvotes: 1

Views: 1745

Answers (3)

Harshith Thota
Harshith Thota

Reputation: 31

The problem here is moment diff function by day is returning the 0 for the both today and tomorrows date. So that the reason it is displaying inside the for the loop when the condition is less than or equal to 0.

var moment =require('moment');

var beforeSevenDays = moment().subtract(7, 'days').format('YYYY-MM-DD');
var i = 0;

for (var m = moment(beforeSevenDays); m.diff(moment(Date.now()), 'days') <0 || m.isSame(moment(), 'day'); m.add(1, 'days')) {
    console.log("i: " + i + " " + m.format('YYYY-MM-DD'));
    i += 1;
}

You can add another condition that where you can check for the same day.

Upvotes: 2

dubes
dubes

Reputation: 5524

The confusion seems to be stemming from how moment constructs days in difference. By subtracting only the date part, it looks like a different day, but you are asking moment to diff the dates. So, what moment does is find the difference in some time units and converts them to days.

As you can see in the snippet below, days diff is 0, but hours diff is not 0. So, you need to freeze the time to something, startOf('day') for example

var beforeSevenDays = moment(Date.now() - 7 * 24 * 3600 * 1000).format('YYYY-MM-DD');
var i = 0;
for (var m = moment(beforeSevenDays); m.diff(moment(Date.now()).local('in'), 'days') <= 0; m.add(1, 'days')) {
  console.log("diff in hours is: " + m.diff(moment(Date.now()).local('in'), 'hours'))
  console.log("diff in days is: " + m.diff(moment(Date.now()).local('in'), 'days'))
  console.log("diff in days with startOf is: " + m.diff(moment(Date.now()).startOf('day').local('in'), 'days'))
  console.log("i: " + i + " " + m.format('YYYY-MM-DD'));
  i += 1;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>

Upvotes: 0

VincenzoC
VincenzoC

Reputation: 31502

The issue with your code is that you are comparing full dates, moment(Date.now()) creates a moment in time that includes date and time, so your output depend on the time when you execute it.

I suggest to use startOf('day') that:

Mutates the original moment by setting it to the start of a unit of time.

Here a live sample:

var beforeSevenDays = moment().subtract(7, 'days').format('YYYY-MM-DD');
var today = moment().startOf('day');
var i = 0;
for (var m = moment(beforeSevenDays); m.diff(today, 'days') <= 0; m.add(1, 'days')) {
    console.log("i: " + i + " " + m.format('YYYY-MM-DD'));
    i += 1;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>

Side note: you can just use moment() to get current date and time, no need to use Date.now(), moreoveryou can use subtract() to get 7 days ago without making math operation, finally there is no need to use local() (do not cofuse it with locale()) since moment are created by default in local mode.

Upvotes: 1

Related Questions