jump to navigation

Always do your calculations based on days !
January 30, 2006

Posted by Roel in : Technical , trackback

This is a simple example of what can happen when you calculate with years only:

Imagine you had a backup script which would delete all old backups, older than 1 year.
When you were using a VBScript (or ASP) function like:
DateDiff('yyyy', backup_create_date, Now)The 1st of January all your backups would be deleted (even the ones created at 31-12-previous_year). This is because Microsoft has quite a different way of doing math with years:
"When comparing December 31 to January 1 of the immediately succeeding year, DateDiff for Year ("yyyy") returns 1 even though only a day has elapsed."
Source: Microsoft MSDN VBScript Functions Reference – DateDiff Function
 
Solution: The best thing to do is always to calculate using days. The example above would be:
DateDiff('d', backup_create_date, Now).
Note: Keep in mind that years have a variable number of days (ie: 365, 366).

Comments»

no comments yet - be the first?



eight + 8 =