Date difference

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.

About these ads

25 Responses to Date difference

  1. Jeff Tapper says:

    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.

  2. udayms says:

    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.

  3. udayms says:

    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.

  4. udayms says:

    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.

  5. Jeff Tapper says:

    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!

    • Richard says:

      welcome to the FLEX world, where any code/solution/example is up for grabs.

      I see my Adobe Forum messages re-distributed all over the web, in languages I dont even speak….

  6. Rakesh Sharma says:

    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

  7. Sam says:

    Leap Years?

  8. Ami says:

    I did not understand this code. Please explain me whoever is the author! I did not get call to the method.

    public static function dateDiff(datePart:String, date1:Date, date2:Date):Number{
    return getDatePartHashMap()[datePart.toLowerCase()](date1,date2);
    }

  9. John says:

    “I came up with this:” no, you didnt

  10. Gkynydlx says:

    I was made redundant two months ago preteen tits torrent 9972

  11. Mznvwtjl says:

    Could you send me an application form? underage gallery
    11683

  12. Imilbmrd says:

    I’ve got a full-time job Loli Top rlmjm

  13. Vkhcvvlk says:

    I’ve got a part-time job http://cuudoykei.de.tl balloon bikini model The guys body is HAWWWTT!!! If only electricians looked like that, I would call them for any excuse very often..

  14. Uosdbljz says:

    Best Site good looking http://eciiemojaj.de.tl nymphets little list 100 Very Good! Don’t often see a women being spunked over whilst having a cock pumping into her cunt at the same time….and it’s obvious that she get’s off on being spunked over! How I’d love to see my wife taking it like that!

  15. Lagan Pillay says:

    Hey I know this is off topic but I was wondering if you knew of any widgets I could add to my blog that automatically tweet
    my newest twitter updates. I’ve been looking for a plug-in like this for quite some time and was hoping maybe you would have some experience with something like this. Please let me know if you run into anything. I truly enjoy reading your blog and I look forward to your new updates.

  16. Hello, I believe your blog could be having web browser compatibility issues.

    Whenever I take a look at your site in Safari, it looks fine but when opening in IE, it’s got some overlapping issues. I just wanted to provide you with a quick heads up! Apart from that, fantastic site!

  17. When it comes to what foods you eat. When we answer
    this question, then dietary recommendations advocated by the paleo
    for athletes program. There is only that many times you can
    eat it. Chinese observed Bigu as it was 50, 000 years ago.

  18. I have 2 different naturally detox your body programs going at this time.
    Many who undertake this fast. For three days, the tumour disappeared.

  19. I really like your blog.. very nice colors & theme. Did you make this website yourself
    or did you hire someone to do it for you? Plz reply as I’m looking to
    construct my own blog and would like to know where u got this from.
    appreciate it

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: