Day home Documentation
Day Methods overview
Negotiating traps and edge cases

Introduction

We need to be able to do more than add a given number of days to a fully specified date. What if one of the dates of a 'which comes first' function is EoT or NK? How do we fudge the conversion between months and days? Which 'comes first' May 2014 or May 14th 2014. What do we actually mean by 'comes first'?

For what follows to make sense you will need to understand the key Day key concepts

Non-linear time problems

Unfortunately for fans of arithmetic, each year doesn't have 13 months of 28 days; instead we have a non-deterministic world of guesswork.

Adding intervals to dates

First the good news. Adding (or subtracting) a given number of days to a fully specified date is easy. (Convert to Julian day number, add the required number of days then convert back.)

If we deal with months then we have a problem

What are the answers to the following (remembering 2008 is a leap year) :
(a) 1st January 2008 plus 1 month? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1st Feb
(b) 21st January 2008 plus 1 month? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21st Feb
(c) 30th January 2008 plus 1 month? . . . . . . . . . . . . . . . . . . . . . . . . 29th Feb or 1st March
(d) 31st January 2008 plus 1 month? . . . . . . . . . . . . . . . . . . . . . . . . 29th Feb or 2nd March
(e) 28th January 2009 plus 1 month? . . . . . . . . . . . . . . . . . . . . . . . . . 28th Feb or 25th Feb
(f) 29th January 2009 plus 1 month? . . . . . . . . . . . . . . . 28th Feb or 1st March or 26th Feb
(g) 30th January 2009 plus 1 month? . . . . . . . . . . . . . . 28th Feb or 2nd March or 27th Feb
(h) 31st January 2009 plus 1 month? . . . . . . . . . . . . . . . . . . . . . . . . 28th Feb or 3rd March
(i) 29th February 2008 plus 1 year? . . . . . . . . . . . . . . . . . . . . . 28th Feb or 1st March 2009

Are we all agreed with (a)? It is pretty reasonable that one month after the first of any month will be the first of the next month. And so for the 2nd, 3rd and so on... What about the last day of the month? Shouldn't a one month addition map to the last day of the next month? By this reasoning (d) and (h) should give the last day of February. (Different because 2008 is a leap year.) What about the 2nd and 3rd from last days? Shouldn't these map to the 2nd and 3rd from last days of the next month? (f) is 2 days before the end of January so surely the result should be two days before the end of February?

The moral of the story is if your project is delayed by a 'month', get that changed to 28 days or 4 weeks.

Partial dates

2019 plus -3y 0m 0d is clearly 2016.
May 2010 plus +1y 3m 0d is clearly August 2011
May 2010 plus +0y 1m 0d is clearly June 2010
May 2010 plus +0y 0m 31d might seem to be June 2010 but WE CAN'T DO IT. Frustrating isn't it! 29, 30, 31, 32 days? We just can't be sure. So it looks like we may need some fudge-rules when we're dealing with partial precision, or we may have to outlaw certain combinations.

Addition rules

Three addition (subtraction is just a negative) methods are provided.

Day.AddDays(Integer number of days)

NVI,NV,NK,BoT,EoT return a copy of themselves.

Cal attempts to convert to the equivilent Julian day number and if it succeeds then adds the required number of days then converts back. This fails though if the date isn't fully specified and returns NV.

INT and FLO use the DAYSINMONTH constant (30.4375) to convert the date to days which are then added as you'd expect then converted back