Flex Date Utils – DateDiff fix
I thought I would take a small break in the middle of my posts for the Flex Date Utils library to go over a fix I just had to make to the dateDiff method in the DateUtils class. I hadn’t really noticed this issue until I was reviewing my code for another fix I had put in previously (that was also not working correctly).
The main issue that cropped up was that to get the year, I was just subtracting the endDate year from the startDate year. However, when you think about this, it is a very inaccurate way of treating year. As year and months can vary in length, I couldn’t just use the Milliseconds contained within it to determine whether a year or month had occurred. I had to just subtract one year or month from another. However, if 2 months fall on separate years, but are not > 1 year apart, it will return 1, even though they could only be a month or so apart. e.g. December 2008 to February 2009 is nowhere near 1 year apart, but subtracting 2008 from 2009, returns 1.
In order to fix this problem, I decided to check the total months that had passed also. I then ran into yet another snag. What if the month was the same, but the day of the month had not passed. e.g. July 16 2007 to July 5 2008 would not be 1 year, although if just using the months and year, it might suggest that it was 1 year apart. To fix this problem, I decided to also check the milliseconds difference between the 2 dates. This now fixes any dates that have different times within them also.
The dateDiff( MONTH ) was having a similar issue so this now fixes that problem also.
I put this fix up on the FlexDateUtils code and also updated the demo so that the issue would be fixed there also.
Make sure to download the latest code and you shouldn’t have any more issues with the dateDiff.
This Utility seems to be working awesome. Good work and thank you!
Thanks. Now I’m trying to figure out if there’s anything else I can add to it. Don’t want it to get too bogged down with methods, but if there’s anything else you can think of that may be helpful, feel free to post.
Hey
Thanks soooo much for this utility really needed it. But, its giving me a problem.
Was using dateDiff to find number of months.
When I enter the dates as April 01, 2009 to April 01, 2016 it returns 84 months, which is correct.
But if I enter April 1, 2009 to March 31, 2010 it returns 11 months :-S
Shouldn’t it be returning 12 ??
The utility is actually using the time in there as well. So it’s actually going from April 1, 2009 00:00:00 to March 31, 2010 00:00:00, so you will end up being 1 day off of one year, thus only 11 months. I tried to make it as specific as possible for the date calculations.
Oh, ok that explains it. Thanks for the reply and the awesome util. Makes life so much simpler.
I have downloaded 1.0.4 and 1.0.5 (according to the zip file names) and both seem to be doing the same thing. If my start date is 2/27/2008 and my end date is 2/16/2010, doing a diff using the full year should result in 1. But both versions give me 2. I thought your write-up meant that you have accounted for partial years. Am I getting the correct version of the code? The only difference I see between 1.0.4 and 1.0.5 is a mod on 7/2/2009 for Daylight Savings Time.
@Suzanne Dorman
Hi Suzanne,
I’ve updated the google code base with the 1.0.8 version. I added it to riaforge, but forgot to update google. I ran some test cases with the dates you provided and it seems to be working correctly in 1.0.8. Please let me know if you experience something different,
Thanks,
Gareth
@Gareth
Hi Gareth, version 1.0.8 works correctly. Thank you so much for all the work you did! FYI – I tried to download it from this site but got an error when opening the zip file. I was able to get it from riaforge.
@Suzanne Dorman
Argh! Didn’t try out the zip before I uploaded it. I uploaded the one from riaforge to Google and that seems to have fixed it. Thanks for the info. Hope the date utils are helping