-1

I have a start date [dates are in y-m-d format] (2018-12-23) and an end date(2018-12-31). I want those dates only between the start and end date where the day is Monday,Thursday,Saturday. How can I achieve this using javascript only ?

I have done it in PHP, but I need this in node js.

my PHP code sample is:-

<?php
$startDate = '2018-12-23';
$endDate = '2018-12-31';
for ($i = strtotime($startDate); $i <= strtotime($endDate); $i = strtotime('+1 day', $i)) {
  if (date('N', $i) == 1 || date('N', $i) == 2)
    echo date('l Y-m-d', $i);
}
?>
Sandip Nag
  • 968
  • 2
  • 18
  • 34

1 Answers1

2

are you looking for something like this?

let getDatesByDay = (start_date, end_date) => {
    let datesArray = [];
    const date1_date = new Date(start_date);
    const date2_date = new Date(end_date);
    let current_date = date1_date;
    while (current_date <= date2_date) {
        if(filterDateByDay(current_date)){
            datesArray.push(`${current_date.getFullYear()}-${current_date.getMonth()+1}-${current_date.getDate()}`)
        }
        current_date = new Date(current_date.setDate(current_date.getDate()+1));
    }
    console.log(datesArray);
}

let filterDateByDay = (date) => {
    const currentDay = date.getDay();
    const acceptableDays = [1,4,6]
    if(acceptableDays.indexOf(currentDay) != -1)
        return date;
    return null;
} 

getDatesByDay("2018-11-01","2018-12-31");
Bala
  • 1,129
  • 1
  • 9
  • 23