score:0

var start = new date('march 1, 2020');
var period = [];

for(var i=1; i<=60; i++){

  if( i == 32 )
    start = new date('april 1, 2020');

  if( i <= 31 )
    start.setdate(i);  
  else
    start.setdate(i - 31);  

  var datestring = start.todatestring().split(' ');

  period.push({
    day: datestring[0], 
    date: datestring[2], 
    month: datestring[1], 
    year: datestring[3]
  });
  
}

console.log( json.stringify(period) );

score:0

i'm assuming you have two dates as your range delimiters. if not, you can create them this way:

var startdate = new date('1 march 2020')
var enddate = new date('29 april 2020')

then, you have to increase the first date by one day until you reach the last date. to increase the first date by one day you can do this:

startdate.setdate(startdate.getdate() + 1)

you can get the day and the month from a date with date.getday() and date.getmonth(), but those methods will return numbers. to get the actual names you can do this:

var days = ["sun", "mon", "tue", "wed", "thu", "fri", "sat"]
var months = ["jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"]

var startdateday = days[startdate.getday()]
var startdatemonth = months[startdate.getmonth()]

and then, you iterate:

var period = []

while (startdate <= lastdate) {
  period.push({
    day: days[startdate.getday()],
    date: startdate.getdate(),
    month: months[startdate.getmonth()],
    year: startdate.getyear()
  })
  startdate.setdate(startdate.getdate() + 1)
}

and that's it. here's a fiddle with a working example:

var startdate = new date('1 march 2020')
var enddate = new date('29 april 2020')

var days = ["sun", "mon", "tue", "wed", "thu", "fri", "sat"]
var months = ["jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"]

var period = []

while (startdate <= enddate) {
  period.push({
    day: days[startdate.getday()],
    date: startdate.getdate(),
    month: months[startdate.getmonth()],
    year: startdate.getfullyear()
  })
  startdate.setdate(startdate.getdate() + 1)
}

console.log(period)

score:1

in traditional way you can do it like this

    var startdate = new date('2020-03-10');
    var enddatedate = new date('2020-03-12');
    var arr = [];

    var days = ['sunday','monday','tuesday','wednesday','thursday','friday','saturday'];

    while(startdate.gettime() !== enddatedate.gettime()) {
       startdate.setdate(startdate.getdate() + 1)
       arr.push({
          day: days[startdate.getday()], 
          date: startdate.getdate(), 
          month: startdate.getmonth(), 
          year: startdate.getyear()
       })
    }

    console.log(arr);

score:2

you can create array from date range follow https://stackoverflow.com/a/50398144/4964569

and get day in date follow https://stackoverflow.com/a/4822882/4964569

and use map function to generate your required

var getdaysarray = function(s,e) {for(var a=[],d=new date(s);d<=e;d.setdate(d.getdate()+1)){ a.push(new date(d));}return a;};

var daterange = getdaysarray(new date('2020-03-10'), new date('2020-04-29'));
var days =  ['sun','mon','tue','wed','thu','fri','sat'];

var result = daterange.map(function(elem){

     var obj =  {
       day: days[elem.getday()],
       date: elem.getdate(),
       month: elem.getmonth(),
       year: elem.getfullyear()

     }

     return obj;
});

console.log(result)

var getdaysarray = function(s,e) {for(var a=[],d=new date(s);d<=e;d.setdate(d.getdate()+1)){ a.push(new date(d));}return a;};

var daterange = getdaysarray(new date('2020-03-10'), new date('2020-04-29'));
var days = ['sun','mon','tue','wed','thu','fri','sat'];

var result = daterange.map(function(elem){
  
     var obj =  {
       day: days[elem.getday()],
       date: elem.getdate(),
       month: elem.getmonth(),
       year: elem.getfullyear()
      
     }
     
     return obj;
});

console.log(result)


Related Query

More Query from same tag