1

https://jsfiddle.net/x991kcus/

I'm able to generate dates array given a start date and end date as params, but what I want is actually array of object like this

[{date:"2017-4-01"},{{date:"2017-4-02"}}]

Below code gave me array.

function generateDates(startDate, stopDate) {
  var dateArray = [];
  var currentDate = moment(startDate);
  var stopDate = moment(stopDate);
  while (currentDate <= stopDate) {
    dateArray.push(moment(currentDate).format('YYYY-MM-DD'))
    currentDate = moment(currentDate).add(1, 'days');
  }

  return dateArray;
}
3
  • Before pushing to the array, create an object o and assign the date to o.date. Then push the o to the array. Commented Apr 25, 2017 at 7:57
  • change dateArray.push({date : moment(currentDate).format('YYYY-MM-DD') }) like this jsfiddle.net/x991kcus/2 Commented Apr 25, 2017 at 7:58
  • You should use date.isAfter(date2) instead of <= Commented Apr 25, 2017 at 8:07

4 Answers 4

2

You could push an object with the generated date.

dateArray.push({ date: moment(currentDate).format('YYYY-MM-DD') });
//             ^^^^^^^                                          ^ wrap in object

const start_date_of_month = moment().format("YYYY-MM-01"),
      end_date_of_month = moment().format("YYYY-MM-") + moment().daysInMonth();

function generateDates(startDate, stopDate) {
    var dateArray = [],
        currentDate = moment(startDate),
        stopDate = moment(stopDate);

    while (currentDate <= stopDate) {
        dateArray.push({ date: moment(currentDate).format('YYYY-MM-DD') });
        currentDate = moment(currentDate).add(1, 'days');
    }
    return dateArray;
}

console.log(generateDates(start_date_of_month, end_date_of_month));
.as-console-wrapper { max-height: 100% !important; top: 0; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>

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

Comments

1

You can push an object to the dateArray

function generateDates(startDate, stopDate) {
  var dateArray = [];
  var currentDate = moment(startDate);
  var stopDate = moment(stopDate);
  while (currentDate <= stopDate) {
    obj = {};
    obj.date = moment(currentDate).format('YYYY-MM-DD')};
    dateArray.push(obj)
    currentDate = moment(currentDate).add(1, 'days');
  }
  return dateArray;
}

Comments

0

Solution using a Object Constructor in JavaScript is a standard way to create an "object type"

function dateObject(date) {
    this.date= date;
}

function generateDates(startDate, stopDate) {
  var dateArray = [];
  var currentDate = moment(startDate);
  var stopDate = moment(stopDate);
  while (currentDate <= stopDate) {
    dateArray.push(new dateObject(moment(currentDate).format('YYYY-MM-DD')))
    currentDate = moment(currentDate).add(1, 'days');
  }

  return dateArray;
}

Working Fiddle Here

Comments

0

https://jsfiddle.net/x991kcus/4/

while (currentDate <= stopDate) {
    var dateObject = {};
    dateObject.date = moment(currentDate).format('YYYY-MM-DD');
    dateArray.push(dateObject);
    currentDate = moment(currentDate).add(1, 'days');
}

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.