DelphiFAQ Home Search:

Calculate the differece between two dates


comments3 comments. Current rating: 5 stars (1 votes). Leave comments and/ or rate it.

Here's a function that calculates the difference between 2 dates that are available as 3 integers (e.g. 5th of April 2003 would be passed as 2003,4,5).

This routine does not take leap years into account.

@monthnames = ('Jan','Feb','Mar',
@monthdays = (0,31,59,90,120,151,181,212,243,273,303,334);

# difference between 2 dates:
# 2003-8-1   and    2003-7-29   -->
# ddiff(2003,8,1,2003,7,29) = 3
sub ddiff {
  local($ny,$nm,$nd,$oy,$om,$od) = @_;
  return ($ny-$oy) * 365 + $monthdays[$nm-1]-$monthdays[$om-1] + ($nd-$od);

Content-type: text/html


2007-01-26, 09:21:53
anonymous from Falls Church in Virginia, United States  
This article gave the exact formula i was looking for after 2 days of my rigorous search of date functions on web for perl. Thankyou so much.

2008-10-07, 11:09:57
This was of great help to me also! I know there are other methods using modules etc, this is what I needed. Thanks very much for this!

Ps: The word difference is spelt incorrectly in the title of the article.. it's spelt 'differece'
2015-01-31, 23:52:52
Filnyal! This is just what I was looking for.



NEW: Optional: Register   Login
Email address (not necessary):

Rate as
Hide my email when showing my comment.
Please notify me once a day about new comments on this topic.
Please provide a valid email address if you select this option, or post under a registered account.

Show city and country
Show country only
Hide my location
You can mark text as 'quoted' by putting [quote] .. [/quote] around it.
Please type in the code:

Please do not post inappropriate pictures. Inappropriate pictures include pictures of minors and nudity.
The owner of this web site reserves the right to delete such material.

photo Add a picture: