Date Calculator

[Home]   [Puzzles & Projects]    [Delphi Techniques]   [Math topics]   [Library]   [Utilities]

 

Search

Search WWW

Search DelphiForFun.org

As of October, 2016, Embarcadero is offering a free release of Delphi (Delphi 10.1 Berlin Starter Edition ).     There are a few restrictions, but it is a welcome step toward making more programmers aware of the joys of Delphi.  They do say "Offer may be withdrawn at any time", so don't delay if you want to check it out.  Please use the feedback link to let me know if the link stops working.

 

Support DFF - Shop

 If you shop at Amazon anyway,  consider using this link. We receive a few cents from each purchase.   Thanks.


Support DFF - Donate

 If you benefit from the website,  in terms of knowledge, entertainment value, or something otherwise useful, consider making a donation via PayPal  to help defray the costs.  (No PayPal account necessary to donate via credit card.)  Transaction is secure.

Contact

Feedback:  Send an e-mail with your comments about this program (or anything else).

Search DelphiForFun.org only

 

 

Problem Description

Explore date calculation algorithms by writing a  program  to emulate the Date Calculator  included with Window 7.

Background & Techniques

There's certainly no great need for another date calculator, but the problem of performing date arithmetic based on Year,  Month, and Day differences seemed like an interesting and solvable one.   Given two dates, the calculator will return the difference in Y(ears), M(onths), W(eeks), and D(ays).  I'll refer to this as a YMD calculation from now on because weeks are easily derived from a days value by dividing by 7 with any remainder being the new days value.  YMD arithmetic is is not commutative, that is applying the operation in a different order will frequently produce a different result because of differing month lengths.  For example, moving from Sept 30 to Nov 1: The difference is 1M, 2D because +1M gets us to Oct 30 and +2D to Nov 1.  But in reverse order Sept 30 + 2D = Oct 2 and Oct 2 + 1M = Nov 2. Both the W7 and DFF calculators calculate and apply differences in YMD order.

In testing, I found some cases where my answer does not agree with W7's answer which probably means that mine are wrong but I haven't figured out why.  I'll list a couple here and I'm sure viewers will let me know who is correct:

bulletCase1:  Date1: 3/19/2013, Date2: 3/30/1997
W7 diff: 15Y, 11M, 2W, 5D  and  Date1 - 15Y -11M - 5D = 3/28/1997
DFF diff:  15Y, 11M, 2W,  6D  and  Date1 - 15Y -11M - 6D = 3/30/1997
bulletCase2: Date1: 3/11/2006, Date2: 12/31/2005
W7 diff:  2M, 1W, 4D and Date1 - 2M - 11D = 12/28/2005
DFF diff: 2M, 1W, 4D and Date1 - 2M - 11D = 12/31/2005
The odd thing about the W7 results in this case is that Date2 values of 12/31, 12/30, 12/29, and 12/28/2005 all return the same difference!  This one looks like a W7 bug to me.

Non-programmers are welcome to read on, but may want to jump to bottom of this page to download the executable program now.

Programmer's Notes:

I just counted, and my notebook has 21 pages of manual date calculation scribblings while writing this program giving you the idea that it was not a trivial project.  For the date differences, I finally broke the calculation into 8 cases. based whether each of the Date1  Y, M, D values were less than or greater than the Date2 Y, M, D values.  The equal YMD values are assigned to one of the groups based on whether move is forward or backward in time.  Each of the cases was then debugged independently.   

It provided a good chance to become acquainted with some of the Delphi DateUtils unit routines  such as IncYear, IncMonth, IncDay to apply year, month, or day increments forward or backward as well as EndOfTheMonth and DaysInAMonth functions.

The user interface is similar to the W7 calculator interface with the exception that I kept a "Test" button used for debugging.  

The "Debug" dialog was added to calculate differences for random date pairs and the reapply the differences to the first date to recreate the second date.  Errors between the input and the calculated Date2 greater than a specified amount are reported.  There are other options in the dialog to control whether to pause on each error and whether to make the sequence repeatable or use new random set for each run.  The Test button runs up to 1000 date pair calculations for each run.    

Running/Exploring the Program 

bulletDownload source
bulletDownload  executable

Suggestions for Further Explorations

???
.
 
Original:  September 11, 2010

Modified:  February 18, 2016

 
  [Feedback]   [Newsletters (subscribe/view)] [About me]
Copyright 2000-2017, Gary Darby    All rights reserved.