views:

302

answers:

4

Is there any ActionScript class which represents "durations of time", similar to the TimeDelta class in Python?

Edit: Thanks for the responses. I should clarify a bit, though: I want be able to ask questions like "how many weeks are between date0 and date1" or "let x represent "one day". What is date2 + x?"

I know I can do all this by representing dates as timestamps... But I'm hoping to find something nicer.

+1  A: 

You can use time() in the Date class to get unix era milliseconds and use that for time delta.

toastie
+1  A: 

If you subtract two dates:

var dateDiff = date1 - date2;

dateDiff will hold the number of milliseconds between the two dates. You can then convert from milliseconds to whatever useful number you like.

Justin Niessner
+1  A: 

I don't think there is a class which measures change in time in Actionscript 3. According to this blog post on Adventures in Actionscript, timing is very inaccurate in the Flash player on the web. That post is pretty informative and has a class called SuperTimer that might help you. You might want to keep this inaccuracy in mind if using solutions posed by Justin Niessner and toastie.

Hooray Im Helping
I'm not worried about timer accuracy – I'm dealing with dates ("there is a meeting at 4:00 on Monday" type of dates). Thanks for the link, though.
David Wolever
+1  A: 

I posted a full AS3 port of the .NET TimeSpan class on this question, which sounds exactly like what you need.

// 5 days from new
var ts : TimeSpan = TimeSpan.fromDays(5);
var now : Date = new Date();
var fiveDaysTime : Date = ts.add(now);

// Diff between dates
var d1 : Date = new Date(2009, 1, 1);
var d2 : Date = new Date(2009, 1, 6);
var ts : TimeSpan = TimeSpan.fromDates(d1, d2);
Richard Szalay
Ah, that looks like exactly it. Thanks!
David Wolever