1

I originally had this working by having something like

var x = endDay - startDay;

for(//iterate for x)  
{
  if(startMonth = endMonth){
     //Push items to array
  }
}

Then I realised if I take a date from lets say 28 December 2021 -> 4 January 2022 that doesnt work.

So i'll show my code but generally this is more of a logic issue then actual code not working..

  createSpecial(startDate, endDate){

  
    var startValue = startDate.split("/");
    var startDay = startValue[0];
    var startMonth = startValue[1];
    var startYear = startValue[2];
   
    var endValue = endDate.split("/");
    var endDay = endValue[0];
    var endMonth = endValue[1];
    var endYear = endValue[2];


    var diff = parseInt(endDay) - parseInt(startDay);
    let difference: Range[] = [];
    var startdate: string = startDay.toString();
    
    for (let i = 0; i <= diff; i++) {
     
        var x = parseInt(startdate);
        x++;
        var s = startdate.toString();
        s = s.padStart(2, '0');
        startdate = x.toString();
        difference.push({
          day: s,
          month: startMonth,
        });
    }
    
export class range{
  day: string;
  month: string;

}

Now i'm extremely unclear about how I can approach this, How can I get a value from the startDate and endDate to use to iterate and create the range in between for later use.

I would essentially just need to get how many days are between the start and end, I'll figure out how to iterate onto a next month afterwords

Edit: Comment pointed me to another question which answered this for me: https://stackoverflow.com/questions/4413590/javascript-get-array-of-dates-between-2-dates#:~:text=Try%20this%2C-,remember,-to%20include%20moment

BeepBopImaRobot
  • 113
  • 1
  • 1
  • 9
  • Does this answer your question? [Javascript - get array of dates between 2 dates](https://stackoverflow.com/questions/4413590/javascript-get-array-of-dates-between-2-dates) – Kaustubh Khare Feb 11 '21 at 07:47
  • Ooh I did not manage to stumble upon that I believe potentially so, Quickly working threw one of the answers then ill possibly close if this ends up being a redundant question – BeepBopImaRobot Feb 11 '21 at 07:53
  • i recommend you https://www.npmjs.com/package/dayjs A library 2kb for javascript/typescript to manipulate dates. – Guiditox Feb 11 '21 at 07:54
  • @Guiditox This would've been great some weeks ago i'll work threw it and see if I can maybe recude my codebase with it thank you – BeepBopImaRobot Feb 11 '21 at 07:57
  • @KaustubhKhare That did resolve my question thank you for sharing – BeepBopImaRobot Feb 11 '21 at 08:08

4 Answers4

1

If you just need the number of days between these 2 days, you can simply do:

let t1 = start.getTime()
let t2 = end.getTime()

let numberOfDaysBetween = (t2 - t1) / 86400000
meshkati
  • 1,720
  • 2
  • 16
  • 29
1

Use the Date type

function getDayDiff(a, b) {
    return Math.abs(a.getTime() - b.getTime()) / (1000 * 60 * 60 * 24)/*1d in ms*/;
}
getDayDiff(new Date(2020, 0), new Date(2021, 0)); // 366
samuelcolt
  • 253
  • 1
  • 5
0

You can try next. Use Date object for date

function monthDiff(firstDate, secondDate) {
let months;
months = (secondDate.getFullYear() - firstDate.getFullYear()) * 12;
months -= firstDate.getMonth() + 1;
months += secondDate.getMonth();
return months <= 0 ? 0 : months;
}
the_zhorov
  • 51
  • 1
  • 6
0

Why not use date-fns ? Unlike moment it won't explode your size budget. It just imports functions you'll be using.

npm i date-fns@2.16.1 --save

then

import { addDays, eachDayOfInterval } from "date-fns";
let dates = eachDayOfInterval(
      {
        start: new Date(),
        end: addDays(new Date(), 10)
      },
      { step: 1 }
    );
  }
Piero
  • 1,638
  • 1
  • 13
  • 14