4 Replies Latest reply on Oct 23, 2007 8:16 AM by Giribabums

# Date substraction

Hi,
I am storing mindate and maxdate in an arraycollection, i want to find number of days between mindate and maxdate.
How can i achieve this, i have written the following code to retrieve the date from arraycollection.

[code]
var tmpStr1:String = oppdata[oppNumber].MinDate; // oppdata is arraycollection
var tmpStr2:String = oppdata[oppNumber].MaxDate;

var tmpDate1:Array = tmpStr1.split("-");
var tmpDate2:Array = tmpStr2.split("-");

var minDate:Date = new Date(tmpDate[1]+"/"+tmpDate[2]+"/"+tmpDate[0] ); // 'MM/DD/YYYY' format;
var maxDate:Date = new Date(tmpDate[1]+"/"+tmpDate[2]+"/"+tmpDate[0] ); // 'MM/DD/YYYY' format;

[/code]

Regards
Giribabu
• ###### 1. Re: Date substraction
Hi,
have a look at http://jeff.mxdj.com/dateadd_for_actionscript.htm. The basic idea is to get the difference in msecs (using maxDate.getTime() - minDate.getTime()) and then "process" the result to the unit (e.g. days) you need.

Uwe
• ###### 2. Date substraction
Hi Flexu,

I did it, i forgot to divide the days with the millisecondperday. lateron i realized and changed the code now its working fine. Thanks for your valiable reply

[code]
public static const millisecondsPerDay:int = 1000 * 60 * 60 * 24;

private function myDaysDifference(minDate:Date, maxDate:Date):uint
{
// for testing
// var minDate:Date = new Date(2007,9,23);
// var maxDate:Date = new Date (2007, 8, 23);
return Math.ceil(( minDate.getTime() - maxDate.getTime()) / millisecondsPerDay);
}
[/code]

Regards
Giribabu
• ###### 3. Re: Date substraction

Here is a useful method I wrote for a project:

public function getDateOffset(dateStart:Date, dateFinish:Date):Date
{
var resultingDate:Date = new Date();
var timeOffset:uint;

var numHours:uint;
var numMinutes:uint;
var numSeconds:uint;

timeOffset = dateFinish.getTime() - dateStart.getTime();

// 1 minute is 60,000 milliseconds

numHours = Math.floor(timeOffset / 60 / 60000);
numMinutes = Math.floor((timeOffset - (numHours * 60 * 60000)) / 60000);
numSeconds = Math.floor((timeOffset - (numHours * 60 * 60000) - (numMinutes * 60000)) / 1000);

resultingDate.setHours(numHours);
resultingDate.setMinutes(numMinutes);
resultingDate.setSeconds(numSeconds);

return(resultingDate);

}
• ###### 4. Re: Date substraction
Hi Karl,

Thanks for your valuable reply, may be your code will be usefull in feature, you can find the code above marked as answer for my question.

Regards
Giribabu