From JavaScript is there a way to get list of days between two dates from MySQL format. I don't want to use any library for this. This is what i did.
function generateDateList(from, to) {
    var getDate = function(date) { //Mysql Format
        var m = date.getMonth(), d = date.getDate();
        return date.getFullYear() + '-' + (m < 10 ? '0' + m : m) + '-' + (d < 10 ? '0' + d : d);
    }
    var fs = from.split('-'), startDate = new Date(fs[0], fs[1], fs[2]), result = [getDate(startDate)], start = startDate.getTime(), ts, end;
    if ( typeof to == 'undefined') {
        end = new Date().getTime();
    } else {
        ts = to.split('-');
        end = new Date(ts[0], ts[1], ts[2]).getTime();
    }
    while (start < end) {
        start += 86400000;
        startDate.setTime(start);
        result.push(getDate(startDate));
    }
    return result;
}
console.log(generateDateList('2014-2-27', '2014-3-2'));
I test it from chrome and nodejs below are the result.
[ '2014-02-27',
  '2014-02-28',
  '2014-02-29',
  '2014-02-30',
  '2014-02-31',
  '2014-03-01',
  '2014-03-02' ]
yeh big leap year:-D..., how can i fix this? or is there any better way.?
const listDate = [];
const startDate ='2017-02-01';
const endDate = '2017-02-10';
const dateMove = new Date(startDate);
let strDate = startDate;
while (strDate < endDate) {
  strDate = dateMove.toISOString().slice(0, 10);
  listDate.push(strDate);
  dateMove.setDate(dateMove.getDate() + 1);
};
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With