Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to identify if a date is between October 1 to May 31 irrespective of the year in Javascript

Tags:

javascript

So I am working on a JavaScript application where I need to calculate monthly charges of utilities consumed. The utility bills have different rates

  • winter rates (June 1 to September 30) --> higher rates
  • summer rates (October 1 - May 31)

So when I need to calculate a bill for the month of lets say : April 25 - May 22 then this needs to be determined as Summer.
So I need to calculate if the billing date is in summer or in winter.

I am trying to do some something of the following form , but this is a lot of juggling in code.

How do I calculate if a given date in between say October - May ( Summer ) or between June - September ( winter )

Note - I need to do this ignoring year

if (
  dateFrom.getMonth() >= vendorSummerStartDate.getMonth() &&
  dateFrom.getDate() >= vendorSummerStartDate.getDate() &&
  dateTo.getMonth() <= vendorSummerEndDate.getMonth() &&
  dateTo.getDate() <= vendorSummerEndDate.getDate()
) { /*... */ }
like image 405
akila Avatar asked Aug 22 '21 05:08

akila


People also ask

How do you check if a date is in between two dates JavaScript?

To check if one date is between two dates with JavaScript, we can compare their timestamps. const currentDate = new Date(). toJSON(). slice(0, 10); const from = new Date("2022/01/01"); const to = new Date("2022/01/31"); const check = new Date(currentDate); console.


1 Answers

It looks tricky when the start month could be a greater number than the end month. We can take abstraction of this, and just consider the involved values (start, end, value) to be any integers (not necessarily months):

  • Check whether the given value lies on or between the start and end values, by checking that value - start and value - end have a different sign, or more precisely, that the product of these terms is either 0 or negative. If 0, then we can conclude the value coincides with either start or end value and is thus in the range.

  • Otherwise check whether the start value is less than the end value

  • If both the above boolean expressions are equal (true - true or false - false), then we can conclude that the value is in the range.

Implementation:

function isInRange (start, end, val) {
    let sign = (val - start) * (val - end);
    return !sign || (sign < 0) == (start < end); 
}

// Wrapper around above function, converting dates to months
const monthIsInRange = (...args) => isInRange(...args.map(dt => dt.getMonth()));

// A few tests:
console.log(monthIsInRange(new Date("2019-06-01"), new Date("2019-09-30"), 
                           new Date("2021-07-15"))); // true
console.log(monthIsInRange(new Date("2019-06-01"), new Date("2019-09-30"), 
                           new Date("2021-05-15"))); // false
console.log(monthIsInRange(new Date("2019-10-01"), new Date("2020-05-31"), 
                           new Date("2021-07-15"))); // false
console.log(monthIsInRange(new Date("2019-10-01"), new Date("2020-05-31"), 
                           new Date("2021-05-15"))); // true
like image 146
trincot Avatar answered Oct 29 '22 01:10

trincot