Home > Software design >  Function to find if date is within period suddenly broken. Anyone able to explain why?
Function to find if date is within period suddenly broken. Anyone able to explain why?

Time:10-10

So I coded this function to test if todays date falls within a given range or period based on my two arrays of start and end days. It then appends the year and tests. If it can't find a period it returns "NIR" or "Not in Range" otherwise it returns a string with the period and year, which I user later in my program for several other functions.

This function worked splendidly and then today randomly it begins returning nothing but NIR. I've tried logging in between each step and I can even get it to work if I set d = Date.parse("Oct 21, 2021") instead of Date.now() it works fine. I am so confused as to what the difference is.

function findPeriod(offset = 0) {
  const s = ["Aug 29", "Sept 12", "Sept 26", "Oct 10", "Oct 24", "Nov 7", "Nov 21", "Dec 5", "Dec 19", "Jan 2", "Jan 16", "Jan 30", "Feb 13", "Feb 27", "Mar 13", "Mar 27", "Apr 10", "Apr 24", "May 8", "May 22", "Jun 5", "Jun 19", "Jul 3", "Jul 17", "Jul 31", "Aug 14", "Aug 28", "Sept 11", "Sept 25"]; // Start of periods
  const e = ["Sept 11", "Sept 25", "Oct 9", "Oct 23", "Nov 6", "Nov 20", "Dec 4", "Dec 18", "Jan 1", "Jan 15", "Jan 29", "Feb 12", "Feb 26", "Mar 12", "Mar 26", "Apr 9", "Apr 23", "May 7", "May 21", "Jun 4", "Jun 18", "Jul 2", "Jul 16", "Jul 30", "Aug 13", "Aug 27", "Sept 10", "Sept 24", "Oct 8"]; // End of periods
  const y = '2021';
  const p = ["Period 19", "Period 20", "Period 21", "Period 22", "Period 23", "Period 24", "Period 25", "Period 26", "Period 27", "Period 1"]; // Corresponding period

  const d = Date.now();

  let found = " ";

  try {
    for (let i = 0; i < p.length; i  ) {
      if (d >= Date.parse(s[i]   ', '   y) && d <= Date.parse(e[i]   ', '   y)) {
        found = p[i   offset]   " "   s[i   offset]   " to "   e[i   offset]   `, ${y}`;
        break;
      } else {
        found = "NIR";
      }
    }

    return found;
  } catch (err) {
    console.log(err);
  }
}

console.log(findPeriod());

CodePudding user response:

Date.now() gives you a date with hours, minutes and seconds. That's why today (9th of October time) is larger than the end of the period you defined.

Quick and dirty. Get the date without time by converting to a date string first:

const d = new Date(new Date().toDateString());

    function findPeriod(offset = 0) {
        
          const s = ["Aug 29", "Sept 12", "Sept 26", "Oct 10", "Oct 24", "Nov 7", "Nov 21", "Dec 5", "Dec 19", "Jan 2", "Jan 16", "Jan 30", "Feb 13", "Feb 27", "Mar 13", "Mar 27", "Apr 10", "Apr 24", "May 8", "May 22", "Jun 5", "Jun 19", "Jul 3", "Jul 17", "Jul 31", "Aug 14", "Aug 28", "Sept 11", "Sept 25" ]; // Start of periods
      const e = ["Sept 11","Sept 25", "Oct 9", "Oct 23", "Nov 6", "Nov 20", "Dec 4", "Dec 18", "Jan 1", "Jan 15", "Jan 29", "Feb 12", "Feb 26", "Mar 12", "Mar 26", "Apr 9", "Apr 23", "May 7", "May 21", "Jun 4", "Jun 18", "Jul 2", "Jul 16", "Jul 30", "Aug 13", "Aug 27", "Sept 10", "Sept 24", "Oct 8"]; // End of periods
      const y = '2021';
      const p = ["Period 19", "Period 20", "Period 21", "Period 22", "Period 23", "Period 24", "Period 25", "Period 26", "Period 27", "Period 1"]; // Corresponding period
      
      const d = new Date(new Date().toDateString());
    
      let found = " ";
    
      try {
        for (let i = 0; i < p.length; i  ) {
          if (d >= Date.parse(s[i] ', '  y) && d <= Date.parse(e[i] ', '  y)) {
            found = p[i offset] " " s[i offset] " to " e[i offset] `, ${y}`;
            break;
          } else {
            found = "NIR";
          }
        }
        
        return found;
      } catch (err) {
        console.log(err);
      }
    
    }
    
    console.log(findPeriod());

  • Related