1

I got some confusion to get the number of dates falls under 1 month or 3 months or 6 months or particular time period. How to do that?. Date format is MM/DD/YYYY. The date range will be added dynamically. Any one please help out to solve this.

for ex

  • All (10)
  • Last month(6)
  • Last 3 months(3)
  • Last 6 months(1)
  • 12/25/2015 - 01/25/2016 (7)

and my ex Array is

var array = ['01/05/2016','01/08/2016','01/09/2016','01/10/2016','01/11/2016','01/12/2016','12/25/2015','11/25/2015','11/29/2015','10/25/2015'];
var currentDate = '02/04/2016';

I have to loop the array and create li elements. The first li element should tell total count of array and next li element should tell count of dates falls in last one month and next li should tell last 3 month. how to segregate inside loop.

gauti
  • 1,264
  • 3
  • 17
  • 43
  • I have to loop the array and set the date count in "li" elements – gauti Feb 06 '16 at 11:12
  • Ooh yes! Don't you think you are asking for too much ? What have you tried so far ? – Rayon Feb 06 '16 at 11:15
  • I will add the code what i have tried. But there is a problem in count . – gauti Feb 06 '16 at 11:26
  • Please refer this https://jsfiddle.net/28qao9o8/1/ . I have added for static elements. When it comes to certain date range and to be updated in html how to do that. – gauti Feb 06 '16 at 11:57

1 Answers1

1

You can do something like this by finding out the difference between dates

var array = ['01/05/2016', '01/08/2016', '01/09/2016', '01/10/2016', '01/11/2016', '01/12/2016', '12/25/2015', '11/25/2015', '11/29/2015', '10/25/2015', '9/25/2015'];
var currentDate = '02/04/2016';
$('#range').change(function() {
    var range = this.value;
    //get the range value
    var result = array.filter(function(v) {
      // filter arry using filter()
      var diff = Math.abs((new Date(currentDate)).getTime() - (new Date(v)).getTime());
      // parse two dates and get difference between them
      return Math.ceil(diff / (1000 * 3600 * 24)) <= range * 30;
      // get the number of days difference and compare with the range
    });
    $('#list').html('Count : ' + result.length + '<br> Array : <pre> ' + JSON.stringify(result, null, 3) + '</pre>')
      // showing result in json format
  }).change()
  // triggering change event
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<select id="range">
  <option value=1>1</option>
  <option value=2>2</option>
  <option value=3>3</option>
  <option value=6>6</option>
</select>
<div id="list"></div>

Helpful question : Get difference between 2 dates in javascript?

UPDATE : As per your need you can do something similar like this

var array = ['01/05/2016', '01/08/2016', '01/09/2016', '01/10/2016', '01/11/2016', '01/12/2016', '12/25/2015', '11/25/2015', '11/29/2015', '10/25/2015'];
var currentDate = '02/04/2016';
$('#range').change(function() {
    var range = this.value;
    //get the range value
    var result = range == 1 ? getRes(range).length : getRes(range).length - getRes($(':selected', this).prev().val()).length;
    // getting count diff if range is greater than 1
    $('#list').html('Count : ' + result);
    // showing result in json format
  }).change()
  // triggering change event

function getRes(range) {
  return array.filter(function(v) {
    // filter arry using filter()
    var diff = Math.abs((new Date(currentDate)).getTime() - (new Date(v)).getTime());
    // parse two dates and get difference between them
    return Math.ceil(diff / (1000 * 3600 * 24)) <= range * 30;
    // get the number of days difference and compare with the range
  });
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<select id="range">
  <option value=1>1</option>
  <option value=3>3</option>
  <option value=6>6</option>
</select>
<div id="list"></div>
Community
  • 1
  • 1
Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188
  • As per my example, if it is last month it should return 6 and last 3 months thn 3 and last 6 month then its 1. And one important thing is how to do it for dynamic data ranges – gauti Feb 06 '16 at 12:04