Link to home
Start Free TrialLog in
Avatar of CubicleGuy
CubicleGuyFlag for United States of America

asked on

How do I set/initialize a DateField to today's date -7 days?

I have a couple of places where I need to set a DateField to Today - 7 days, and another to Today - 30 days.  Further, the user should not be able to select or input a date greater than the current date.

This should be simple, but the books I'm referencing make it seem convoluted...

Thanks in advance.

Chris
Avatar of Fuzzy_Logic_
Fuzzy_Logic_
Flag of United Kingdom of Great Britain and Northern Ireland image

use date.getTime() this returns a number which is the number of milliseconds since Jan 1st 1970.

Take that number and subtract/add the number of milliseconds you need.

Then use date.setTime(newNumber) with the new number to set the date value.

See example below:
public function addDays(yourDate:Date, value:Number):void // value parameter is the number of days you want to add, use a negative number to subtract days.
 
{
        // get the number of milliseconds since Jan1st 1970
        var numMS:Number = yourDate.getTime();
 
        // multiply value to get equivalent number of milliseconds
        var spanMS:Number = value * 24 * 60 * 60 * 1000; // days * hours * mins * secs * 1000
 
        // add spanMS to numMS to get number of milliseconds since Jan1st 1970
        // if you've used a negative number this will subtract
        var newMS:Number = numMS + spanMS;
 
        // now set yourDate to the new millisecond value
        yourDate = yourDate.setTime(newMS);
 
}
 
        

Open in new window

Avatar of CubicleGuy

ASKER

I was right - it was not intuitive.  


Thanks - I'll work on this immediately and get back with you.
ASKER CERTIFIED SOLUTION
Avatar of zzynx
zzynx
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you - I will be back on this soon.  I wasn't aware that, in terms of the Object parameter for selectableRange(), that you could supply only one (min or max).

thanx 4 axxepting