Swati Singh Swati Singh - 4 months ago 31
ActionScript Question

AS3: Calculate the time difference between two times?

I want to create a function which calculate the time difference between two times.
For example I am sending times as

timedifference(1 AM,5 PM);


How could I calculate the time difference between 1PM to 5PM?
Can anyone help me?

Thanks in advance.

Answer

Here is the answer I want:

public static function timeDifference(startTime:Date, endTime:Date) : String
{
    if (startTime == null) { return "startTime empty."; }
    if (endTime   == null) { return "endTime empty."; }
    //trace(" endTime.valueOf() "+endTime.valueOf()+" startTime.valueOf() "+startTime.valueOf())
    var aTms:* = Math.floor(endTime.valueOf() - startTime.valueOf());
    var timeTaken:* =( int(aTms/(    60*60*1000)) %24 );
    //trace(timeTaken);
    return "Time taken:  "  
        + String( int(aTms/(24*60*+60*1000))     ) + " days, "
        + String( int(aTms/(    60*60*1000)) %24 ) + " hours, "
        + String( int(aTms/(       60*1000)) %60 ) + " minutes, "
        + String( int(aTms/(        1*1000)) %60 ) + " seconds.";
}

Usage of this function as:

var myDate:Date = new Date("3/18/2012 10:00 PM");
var myDate1:Date= new Date("3/19/2012 12:00 PM");

timeDifference(myDate,myDate1);