Home > Enterprise >  Get all specific day in specific end date in JavaScript
Get all specific day in specific end date in JavaScript

Time:09-22

I am trying to get all day (input by me) in a specific end date (input by me).

example :

I have day 1 and I have end date 10/12/2021

beginning from new Date():

output must be: 1/10/2021 1/11/2021 1/12/2021

day 1 for this month is not included because we are in day 20 and day 20 > day 1

Same, I chose day 20 (example) and end date = 19/12/2021 day 20 in end date not included because day 20 > end date day

I have already tried that as simple using if condition, but not great work because in this case, I have 8 conditions!

I want a simple code that works for this situation.

CodePudding user response:

Hope that is what you wanted. You can simplify it more if you want.

const getRemainingDates = function(beginningDay, endDate){
  const today = new Date()
  const [endDateDay,endDateMonth,endDateYear] = endDate.split("/");
  const remainingMonth = endDateMonth - (today.getMonth() 1);
  for(let i = 0; i <= remainingMonth; i  ){
    if(i === 0){
        if(beginningDay>today.getDate() && beginningDay>endDateDay){
          console.log(`${beginningDay}/${today.getMonth() 1 i}/${today.getFullYear()}`)
        }
        else {}
    }
    if(i !== 0)console.log(`${beginningDay}/${today.getMonth() 1 i}/${today.getFullYear()}`)
  }
}

getRemainingDates(21,"12/12/2021");
console.log()
getRemainingDates(1,"12/12/2021");

  • Related