Search code examples
angularjsarraysdate-formatisodate

Adding additional day in array to correct date format in angular


In my angular project, I have a array like this:

Thali : [
  {"date":"2017-04-09T18:30:00.000Z","isHoliday":"100"},
  {"date":"2017-04-10T18:30:00.000Z","isHoliday":"101"}
]

from date picker I selected like first value 2017-04-10 (10 April) and in IsoDate format it shows 1 day less. so need to add 1 day in date but this is array. so how I can add 1 day in all dates of array or able to change format of Isodate?


Solution

  • Here is a solution using pure JavaScript

    var arr = [
      {"date":"2017-04-09T18:30:00.000Z","isHoliday":"100"},
      {"date":"2017-04-10T18:30:00.000Z","isHoliday":"101"}
    ]
    
    arr.forEach(function(entry) {
    	var d = new Date(entry["date"]);
    	// Add one day
    	d.setDate(d.getDate() + 1);
        // Replace existing date in array with the new one
    	entry["date"] = d.toISOString()
    })
    
    console.log(arr);