RoR: Determining two date within the acceptable range

Date Range Picker js

There are two dates where the first date is greater or equal to (gteq) and the second date is less than or equal to (lteq). Both dates set to have a rule where it must be within 3 months range. As always, there are always many ways to determine the range of between gteq and lteq. We will discuss three (3) solutions for this case where the third solution is the best way to consider.

Solution #1

This solution assumes there are 30 days available per month.

As a new Rubyist developer, you feel panicked. Then, you quickly googling how to make it done and the result came out.

You search @google for ways to determine two dates between range in Rails.

As suggested by the stack overflow forum, you are required to do subtraction between two dates. However, that’s not fit your case where you need the difference to be told in months instead of days. Finding the differences by gteq minus by lteq (lteq gteq) will provide a number of days which may not exactly accurate as a number of days in the month is concerned.

Greater than or equal to (gteq)

Less than or equal to (lteq)

Common terms used in date manipulation

Imagine you are comparing between 1 January 2019 and 1 April 2019 while assuming 30 days equivalent to 1 month. Then, you are in luck. However, what if you are on leap year (e.g.: 2020) where you will have an extra 1 day in February. Comparing the same date in the year 2020 will produce 91 days instead of 90 days in 2019.

Lucky! 90 days equivalent to 3 after dividing by 30 in 2019.
How? 91 days is not equivalent to 3 after dividing by 30 in 2020 considering its decimal place value.

Considering all the concerns, Solution #1 may not be the best to determine two dates within the acceptable range. Let’s look for Solution #2.

Solution #2

This solution assumes there are 30.42 days available per month where every year is 365 days and no leap year.

Using an assumption of 30 days available per month is not accurate if the date range is between the first of February (1st February) until the second of March (2nd March). Depending on the leap year cycle, two months may be considered as a month when using 30 days assumption. Hence, you decided not to complicate things further by importing ruby gem where the logic is encapsulated in the class method.

Time Difference Gem GitHub page
Introducing TimeDifference gem!

After navigating to a few websites as listed by google search results, you have come to a gem repository. Until this point, you might be convinced by some argument that why there is missing time difference helper in Ruby by itself. So, here the gem came to rescue.

Seems like you have finally come to an end of comparing if it is within an acceptable range. In the example above, it does assume that you want to check if the range between gteq and lteq is greater than three (3) which will return true if less than or equal to or return false if greater than 3.

Until this point, you feel satisfied where you let the magic happened inside the class itself. At the same time, you are following your ignorance hiding under the name of reuse. Somehow, you came to wonder what magic actually happened and you discover the truth. It’s using the assumption made in Solution #2 where average days available in a month is 30.42.

Relying on the assumption of 30.42 average days available in a month throughout a year is wrong every four years where the average days available in a month are 30.5 days. Solution #2 is not an all-time winner where the assumption is not accurate enough even though it might fit certain use-cases. Let’s look for the final and the best Solution #3.

Solution #3

Do not use any assumption. Increment the gteq month value by three (3) and compare the result with lteq if lteq is greater than the result.

This will be the easiest and the most accurate assumption to be made. Also, this assumption should already in every web developer. It’s simple commonsense to increase the month value and perform a comparison. However, sometimes when there is a lot of things to think, we may simply go for dumb Solution #1 and #2.

Conclusion

I was actually looking for ways to achieve:

  • If the given gteq and lteq is not greater than today.
  • lteq must always greater than gteq or at most equal.
  • The range must be within 2 years period and 3 months in range. Any 3 months range within 2 years period is acceptable.
  • Replace the date with yesterday if the user breached the rules.

In Ruby on Rails controller, set a before action hook to run the logic before it starts executing in specific methods that serve the user request.

If you are the person that requires to do this task, how you will handle it?

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *