0

My javascript array like that.

var datearray = [
    "2016-01-13", 
    "2016-01-18", 
    "2016-01-30", 
    "2016-02-13", 
    "2016-02-18", 
    "2016-02-28", 
    "2016-03-13", 
    "2016-03-23", 
    "2016-03-30", 
    "2016-04-13", 
    "2016-04-18", 
    "2016-04-30", 
    "2016-05-13", 
    "2016-05-18", 
    "2016-05-28", 
    "2016-06-13", 
    "2016-06-23", 
    "2016-06-30", 
    "2016-08-22"
]

but my searching dates are startDate = 2015-12-01; and endDate = 2016-09-30; I want to get new date array between above startDate and endDate. This new array will display like this,

var newOjArray = [
    {"2015-12":"0"},
    {"2016-01":"3"},
    {"2016-02":"3"},
    {"2016-03":"3"},
    {"2016-04":"3"},
    {"2016-05":"3"},
    {"2016-06":"3"},
    {"2016-07":"0"},
    {"2016-08":"1"},
    {"2016-09":"0"}
];

values meaning total count of considering date range. How I created It.

3
  • 1
    And what did you try so far ? Have you at least tried to identify elements in the range of dates or something as basic as this ? Commented Jun 23, 2016 at 10:28
  • I want to get total count of month particular years. Commented Jun 23, 2016 at 10:40
  • I used this one to generate month array , var dateStart = moment("2015-12-01"); var dateEnd = moment("2016-09-30"); var timeValues = []; while (dateEnd >= dateStart) { timeValues.push(dateStart.format('YYYY-MM')); dateStart.add(1, 'month'); } Commented Jun 23, 2016 at 10:42

4 Answers 4

1

A complete proposal. With an array with the wanted grouped result.

function getGroupedData(dates, from, to) {
    function pad(s, n) { return s.toString().length < n ? pad('0' + s, n) : s; }

    var temp = Object.create(null),
        result = [],
        fromYear = +from.slice(0, 4),
        fromMonth = +from.slice(5, 7),
        toYear = +to.slice(0, 4),
        toMonth = +to.slice(5, 7),
        o, k;

    datearray.forEach(function (d) {
        var k = d.slice(0, 7);
        temp[k] = (temp[k] || 0) + 1;
    });

    while (true) {
        k = pad(fromYear, 4) + '-' + pad(fromMonth, 2);
        o = {};
        o[k] = (temp[k] || 0).toString();
        result.push(o);
        if (fromYear === toYear && fromMonth === toMonth) {
            break;
        }
        fromMonth++;
        if (fromMonth > 12) {
            fromMonth = 1;
            fromYear++;
        }
    }
    return result;
}

var datearray = ["2016-01-13", "2016-01-18", "2016-01-30", "2016-02-13", "2016-02-18", "2016-02-28", "2016-03-13", "2016-03-23", "2016-03-30", "2016-04-13", "2016-04-18", "2016-04-30", "2016-05-13", "2016-05-18", "2016-05-28", "2016-06-13", "2016-06-23", "2016-06-30", "2016-08-22"];

console.log(getGroupedData(datearray, '2015-12-01', '2016-09-30'));

Sign up to request clarification or add additional context in comments.

Comments

0

You can use Array.filter to filter through this array. Taking advantage of your particular date format, we do not need to do any date arithmetic, we can simply compare dates as strings and use localeCompare() to compare them:

var datearray = [
    "2016-01-13", 
    "2016-01-18", 
    "2016-01-30", 
    "2016-02-13", 
    "2016-02-18", 
    "2016-02-28", 
    "2016-03-13", 
    "2016-03-23", 
    "2016-03-30", 
    "2016-04-13", 
    "2016-04-18", 
    "2016-04-30", 
    "2016-05-13", 
    "2016-05-18", 
    "2016-05-28", 
    "2016-06-13", 
    "2016-06-23", 
    "2016-06-30", 
    "2016-08-22"
];

var startDate = "2015-12-01"; 
var endDate = "2016-01-30";

var filteredArray = datearray.filter(function(item){
  return item.localeCompare( startDate ) > -1 && endDate.localeCompare( item ) > -1;
});

console.log( filteredArray );

Now, you have the filteredArray and you can simply iterate through it to count the number of dates falling in a month.

3 Comments

each next month of range should be considered, that's why the OP included {"2016-09":"0"} into expected result
@RomanPerekhrest I think you mean I needed to format my filteredArray as specified by OP. But seems like this has solved OP's issue. Thanks.
look's like your answer was sufficient for the OP
0

You may try this:

Underscore.js has been used to manipulate data.

var datearray=["2016-01-13","2016-01-18","2016-01-30","2016-02-13","2016-02-18","2016-02-28","2016-03-13","2016-03-23","2016-03-30","2016-04-13","2016-04-18","2016-04-30","2016-05-13","2016-05-18","2016-05-28","2016-06-13","2016-06-23","2016-06-30","2016-08-22"];

var boxingDay = new Date("12/01/2015");
var nextWeek  = new Date("09/30/2016");

function getDates( d1, d2 ){
  var oneDay = 24*3600*1000;
  for (var d=[],ms=d1*1,last=d2*1;ms<last;ms+=oneDay){
var new_Date=new Date(ms);
    d.push( new_Date.getFullYear()+"-"+("0" + (new_Date.getMonth() + 1)).slice(-2) );
  }
  return d;
}

var x=[];
_.each(datearray, function(e){x.push(e.substring(0, 7));});

var z= _.uniq(getDates( boxingDay, nextWeek ));

var f=x.concat(_.uniq(getDates( boxingDay, nextWeek )));

document.getElementById("xx").innerHTML=JSON.stringify(_.countBy(f));
<script src="http://underscorejs.org/underscore-min.js"></script>

<div id="xx"></div>

Comments

0

If you looking for a more ES6 way then check it out:

var dateArray = ["2016-01-13", "2016-01-18", "2016-01-30", "2016-02-13", "2016-02-18", "2016-02-28", "2016-03-13", "2016-03-23", "2016-03-30", "2016-04-13", "2016-04-18", "2016-04-30", "2016-05-13", "2016-05-18", "2016-05-28", "2016-06-13", "2016-06-23", "2016-06-30", "2016-08-22"];
var group = {};

dateArray.forEach(date =>
  group[(date = date.substr(0, 7))] =
  (group[date] || []).concat(date)
);
var result = Object.keys(group)
  .map(date => ({
    [date]: group[date].length
  }));

console.log(result)

If your date format is as the date array then the easiest way would be to use substr if the length is not constant then you can split it by spacer and then get the two first values. And if it's totally a date string you can create a date from this and convert it to your desired string as key of your object.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.