I've been recently asked to do a simple exercice to calculate the difference between 2 dates.
first date is the birth date, second date is the actual date (or any date entered as the end date) let's call it "end date".
Format: Year, Month, Day (we don't care about hours for now)
Here is the deal :
The birth date : 2021, 02, 11
The end date : 2022, 02, 10
The difference between these 2 dates is : 11 months and 30 days. That's seems the logic answer but let's try to get a little bit deeper :
11/02 -> 11/03 = 1 month : 17 + 11 = 28 }
11/03 -> 11/04 = 1 month : 20 + 11 = 31 |
11/04 -> 11/05 = 1 month : 19 + 11 = 30 |
11/05 -> 11/06 = 1 month : 20 + 11 = 31 | From February
11/06 -> 11/07 = 1 month : 19 + 11 = 30 | To January
11/07 -> 11/08 = 1 month : 20 + 11 = 31 | 11 months
11/08 -> 11/09 = 1 month : 20 + 11 = 31 | => 334 days
11/09 -> 11/10 = 1 month : 19 + 11 = 30 |
11/10 -> 11/11 = 1 month : 20 + 11 = 31 |
11/11 -> 11/12 = 1 month : 19 + 11 = 30 |
11/12 -> 11/01 = 1 month : 20 + 11 = 31 }
11/01 -> 10/02 ------------> 20 + 10 = 30
This is the reasoning behind the 11 months and 30 days, but there is another approach (correct me if I'm wrong) which is :
the starting stays the same, which is 11/02 so the rest is :
02/2021: 17/28
03/2021: 31/31 }
04/2021: 30/30 |
05/2021: 31/31 |
06/2021: 30/30 |
07/2021: 31/31 |
08/2021: 31/31 | 11 months
09/2021: 30/30 | => 337 days
10/2021: 31/31 |
11/2021: 30/30 |
12/2021: 31/31 |
01/2022: 31/31 }
02/2022: 10/28
There is a difference of 3 days between the 2 dates when using the 2nd approach, and therefore the difference will be
11 month and 27 days.
Which approach do you think is the right one ?
I have a requirement where I have say 2 parameters a bucketDelta and a start time and I need to calculate the closest time interval in bucketDelta steps that is less than the given time. (sounds convoluted enough ? Here is an example)
say bucketDelta
15minutes and my time is 13 Sep 7:05 PM - returns 13 Sep 7:00 PM
15minutes and my time is 13 Sep 7:17 PM - returns 13 Sep 7:15 PM
15minutes and my time is 13 Sep 7:35 PM - returns 13 Sep 7:30 PM
...
30 minutes and my time is 13 Sep 7:05 PM - returns 13 Sep 7:00 PM
30 minutes and my time is 13 Sep 7:17 PM - returns 13 Sep 7:00 PM
30 minutes and my time is 13 Sep 7:35 PM - returns 13 Sep 7:30 PM
...
60 minutes and my time is 13 Sep 7:05 PM - returns 13 Sep 7:00 PM
60 minutes and my time is 13 Sep 7:35 PM - returns 13 Sep 7:00 PM
60 minutes and my time is 13 Sep 7:55 PM - returns 13 Sep 7:00 PM
24 Hours and my time is 13 Sep 7:05 PM - returns 13 Sep 12:00 AM
24 Hours and my time is 13 Sep 9:05 PM - returns 13 Sep 12:00 AM
..
Here is the logic I have for this but I am not too happy with a million Ifs. Is there a better way to do this ?
if (bucketDelta == TimeSpan.FromSeconds(900)) {
if (bucketStop.Minute > 0 && bucketStop.Minute < 15)
{
minute = bucketStop.Minute;
}
else if (bucketStop.Minute > 15 && bucketStop.Minute < 30)
{
minute = bucketStop.Minute - 15;
}
else if (bucketStop.Minute > 30 && bucketStop.Minute < 45)
{
minute = bucketStop.Minute - 30;
}
else if (bucketStop.Minute > 45 && bucketStop.Minute < 60)
{
minute = bucketStop.Minute - 45;
}
}else if(bucketDelta == TimeSpan.FromSeconds(1800)) {
if (bucketStop.Minute > 0 && bucketStop.Minute < 30)
{
minute = bucketStop.Minute;
}
else if (bucketStop.Minute > 30 && bucketStop.Minute < 60)
{
minute = bucketStop.Minute - 30;
}
} else if(bucketDelta == TimeSpan.FromSeconds(3600))
{
if (bucketStop.Minute > 0 && bucketStop.Minute < 60)
{
minute = bucketStop.Minute;
}
}
else if (bucketDelta == TimeSpan.FromSeconds(86400))
{
if (bucketStop.Hour > 0 && bucketStop.Hour < 24)
{
minute = (bucketStop.Hour * 60);
}
}
bucketStop = bucketStop.AddMinutes(-1 * minute);
Convert both into integer multiples of a standard unit, divide the absolute time by the length of your bucket, use floor to get an integer, multiply by the length of your bucket, and convert back. Depending on your language, floor may either be implicit from the type system or explicit.
For example in Java your calculation could look something like the following (untested):
long intervals = originalInstant.toEpochMilli() / bucketInMilliseconds;
Instant answer = Instant.fromEpochMilli( bucketInMilliseconds * intervals );
I have following piece of code:
if day > 31
day -= 31
month = "April"
end
Can I write it in one line different than:
if day > 31 then day -= 31 and month = "April" end
?
I've tried it like:
if day > 31 {day -= 31; month = "April"}
But it doesn't work
(day -= 31; month = "April") if day > 31
Alternate way (As suggested by #mudasobwa in comments below) :
day, month = day - 31, "April" if day > 31
I am working on a script that is supposed to determine the "season" of the year based on date ranges:
For Example:
January 1 - April 1: Winter
April 2 - June 30: Spring
July 1 - September 31: Summer
October 1 - December 31: Fall
I am not sure how the best way (or the best ruby way) to go about doing this. Anyone else run across how to do this?
31 September?
As leifg suggested, here it is in code:
require 'Date'
class Date
def season
# Not sure if there's a neater expression. yday is out due to leap years
day_hash = month * 100 + mday
case day_hash
when 101..401 then :winter
when 402..630 then :spring
when 701..930 then :summer
when 1001..1231 then :fall
end
end
end
Once defined, call it e.g. like this:
d = Date.today
d.season
You could try with ranges and Date objects:
http://www.tutorialspoint.com/ruby/ruby_ranges.htm
without ranges.
require 'date'
def season
year_day = Date.today.yday().to_i
year = Date.today.year.to_i
is_leap_year = year % 4 == 0 && year % 100 != 0 || year % 400 == 0
if is_leap_year and year_day > 60
# if is leap year and date > 28 february
year_day = year_day - 1
end
if year_day >= 355 or year_day < 81
result = :winter
elsif year_day >= 81 and year_day < 173
result = :spring
elsif year_day >= 173 and year_day < 266
result = :summer
elsif year_day >= 266 and year_day < 355
result = :autumn
end
return result
end
Neil Slater's answer's approach is great but for me those dates aren't quite correct. They show fall ending on December 31st which isn't the case in any scenario I can think of.
Using the northern meteorological seasons:
Spring runs from March 1 to May 31;
Summer runs from June 1 to August 31;
Fall (autumn) runs from September 1 to November 30; and
Winter runs from December 1 to February 28 (February 29 in a leap year).
The code would need to be updated to:
require "date"
class Date
def season
day_hash = month * 100 + mday
case day_hash
when 101..300 then :winter
when 301..531 then :spring
when 601..831 then :summer
when 901..1130 then :fall
when 1201..1231 then :winter
end
end
end
Given two dates, what is the best method to calculate the number of days between those two dates that fall in a leap year.
For example if d1 = 12/1/2007 and d2 = 1/31/2008 then the total number of days between d1 and d2 would be 62 and the number of days that fall in a leap year would be 31.
Another example is if d1 = 12/1/2007 and d2 = 6/30/2012 then the total number of days between d1 and d2 would be 1674 and the number of days that fall in a leap year would be 548.
I already have function to calculate if a specific year is a leap year and and a function to calculate the number of days between two dates.
If anyone has such a algorithm in Delphi (Pascal) or C/C++/C# that would be greatly appreciated. Any suggestions and assistance would be great.
The solution is in python, and it shouldn't be hard to convert to any other language.
def isLeapYear(year):
if year%4 == 0:
if year%100 == 0:
if year%400 == 0:
return True
else:
return False
else:
return True
else:
return False
def daysBetweenDates(year1, month1, day1, year2, month2, day2):
cumDays = [0,31,59,90,120,151,181,212,243,273,304,334] #cumulative Days by month
leapcumDays = [0,31,60,91,121,152,182,213,244,274,305,335] # Cumulative Days by month for leap year
totdays = 0
if year1 == year2:
if isLeapYear(year1):
return (leapcumDays[month2-1] + day2) - (leapcumDays[month1-1] + day1)
else:
return (cumDays[month2-1] + day2) - (cumDays[month1-1] + day1)
if isLeapYear(year1):
totdays = totdays + 366 - (leapcumDays[month1-1] + day1)
else:
totdays = totdays + 365 - (cumDays[month1-1] + day1)
year = year1 + 1
while year < year2:
if isLeapYear(year):
totdays = totdays + 366
else:
totdays = totdays + 365
year = year + 1
if isLeapYear(year2):
totdays = totdays + (leapcumDays[month2-1] + day2)
else:
totdays = totdays + (cumDays[month2-1] + day2)
return totdays
Here's my pseudo code version using your functions for - is_leap_year, days_between. As a commenter noted, these are tricky functions to write correctly.
int leap_year_days_between(Date d1, Date d2) {
if (d1.year == d2.year) {
if (is_leap_year(d1.year) { return days_between(d1,d2); }
else { return 0; }
}
else {
Date last_day_in_year(12, 31, d1.year);
int count=0;
Date tmp = d1;
while (tmp.year < d2.year) {
if ( is_leap_year(tmp.year) ) {
count += days_between(tmp,last_day_in_year);
}
tmp = (1, 1, tmp.year+1);
}
if ( is_leap_year(d2.year) ) {
count += days_between(tmp, d2);
}
}
}
A naive approach would be:
Check your start year. If it's a leap year, count the number of days from your current day to December 31 (inclusive). If not, until your starting year equals your ending year, increment the year by 1. Then, check the year. If it is a leap year, start counting days, if not increment the year. Once the current year and ending year are the same, then check to see if the current (== ending) year is a leap year. If it is, count days in months from January to the ending month, otherwise break the algorithm. Once your current month is your ending month, count your days.