jquery - How to find number of dates falls under 1 month or 3 months or 6 months in javascript? -
i got confusion number of dates falls under 1 month or 3 months or 6 months or particular time period. how that?. date format mm/dd/yyyy. date range added dynamically. 1 please out 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 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 loop array , create li elements. first li element should tell total count of array , next li element should tell count of dates falls in last 1 month , next li should tell last 3 month. how segregate inside loop.
you can finding out 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 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 2 dates , difference between them return math.ceil(diff / (1000 * 3600 * 24)) <= range * 30; // number of days difference , compare 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 : per need can similar 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 range value var result = range == 1 ? getres(range).length : getres(range).length - getres($(':selected', this).prev().val()).length; // getting count diff if range greater 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 2 dates , difference between them return math.ceil(diff / (1000 * 3600 * 24)) <= range * 30; // number of days difference , compare 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>
Comments
Post a Comment