Script source : [ http://jeff.mxdj.com/datediff_for_actionscript.htm ]
One of the tools I often find myself needing when developing applications in ActionScript is a utility for determining the difference between 2 dates. Following the structure of ColdFusion’s DateDiff function, I came up with this:
class DateFunction {
/**
dateDiff(datePart:String, date1:Date, date2:Date):Number<BR>
returns the difference between 2 dates<BR>
valid dateParts:<BR>
s: Seconds<BR>
n: Minutes<BR>
h: Hours<BR>
d: Days<BR>
m: Months<BR>
y: Years<BR>
*/
public static function dateDiff(datePart:String, date1:Date, date2:Date):Number{
return getDatePartHashMap()[datePart.toLowerCase()](date1,date2);
}
private static function getDatePartHashMap():Object{
var dpHashMap:Object = new Object();
dpHashMap["s"] = getSeconds;
dpHashMap["n"] = getMinutes;
dpHashMap["h"] = getHours;
dpHashMap["d"] = getDays;
dpHashMap["m"] = getMonths;
dpHashMap["y"] = getYears;
return dpHashMap;
}
private static function compareDates(date1:Date,date2:Date):Number{
return date1.getTime() - date2.getTime();
}
private static function getSeconds(date1:Date,date2:Date):Number{
return Math.floor(compareDates(date1,date2)/1000);
}
private static function getMinutes(date1:Date,date2:Date):Number{
return Math.floor(getSeconds(date1,date2)/60);
}
private static function getHours(date1:Date,date2:Date):Number{
return Math.floor(getMinutes(date1,date2)/60);
}
private static function getDays(date1:Date,date2:Date):Number{
return Math.floor(getHours(date1,date2)/24);
}
private static function getMonths(date1:Date,date2:Date):Number{
var yearDiff = getYears(date1,date2);
var monthDiff = date1.getMonth() - date2.getMonth();
if(monthDiff < 0){
monthDiff += 12;
}
if(date1.getDate()< date2.getDate()){
monthDiff -=1;
}
return 12 *yearDiff + monthDiff;
}
private static function getYears(date1:Date,date2:Date):Number{
return Math.floor(getDays(date1,date2)/365);
}
}
there is only one public method to interact with, that is dateDiff. You can use it like this:
var now:Date = new Date();
var kaliBDay = new Date(2004,5,23,11,17);
trace("Diff between "+ kaliBDay + " and now " + DateFunction.dateDiff("y",now,kaliBDay) +" in years");
Following the lead from the CF function, you can pass in dateParts of “y” for years, “m” for months, “d” for days, “h” for hours, “n” for minutes or “s” for seconds. If you want to add new date parts, simply right the method, and reference it in the getDatePartHashMap() method. For example, to add a new date part “ms” for milliseconds, I’d add one more line to getDatePartHashMap which said:
dpHashMap["ms"] = compareDates;
This can be done, as the compareDates method is already written to determine the number of milliseconds between any two dates.
Most of the methods are pretty straight forward, the getMonths is more complex, as the number of days in any given month is different, so it wasnt a simple division based on the millisecond difference, like all the other calculations were.‘ve extended this class a bit more, and added dateAdd() functionality to it as well.



How about giving credit where credit is due. This post was taken word for word from my blog, including my daugthers name and birthday for variables.
hi jeff…..:) plagiarism was never intended. in this blog i post stuff that i find interesting and useful. its mostly like a diary i maintain for myself so that i can refer back when i get stuck at a technical problem. Actually i thought the track back url did that. I moved to wordpress very recently from blogger. I am still trying to figure out things around here. In the post screen, there was a field for “Trackback URL”, and I promptly put in your page URL there, so that it links back to your original article.
See, I never changed the script and variable names because I had no intention of taking away the credit or anything like that
. Anyway thanks for pointing it out. Now that i have learnt that the track back url doesnt work that way, I shall include a url to original article.
ok.. Done…
Sorry jeff for the short misunderstanding. I have included your site url in the entry. Newbies like me use scrtiplets from across the web like the one you have written to learn actionscript. Keep writing.
I’m happy to have folks use the code I write (that is why i post it to my blog, rather than keep it to myself), its just a bit unnerving to read my writings somewhere else, without any indication they are my writings. Thanks for the attribute.
By the way, for things like this, what I do on my blog is post a link to the original page, and add my thoughts, comments etc, on my page, rather than copying over the whole page. This way, if the contents on the other page changes, I dont need to go back and update my page.
Just my $0.02. Good luck with the blog!
I want two know that how to find diffrance between in two dates of two datetime picker.formate is mm/dd/yy in both.
give responce early
Leap Years?