• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2118
  • Last Modified:

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.

  • 2
  • 2
1 Solution
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

CubicleGuyAuthor Commented:
I was right - it was not intuitive.  

Thanks - I'll work on this immediately and get back with you.
zzynxSoftware engineerCommented:
>> Further, the user should not be able to select or input a date greater than the current date.
Set the selectableRange property of your DateField

I quote from http://livedocs.adobe.com/flex/3/langref/mx/controls/DateField.html:
selectableRange property       

selectableRange:Object  [read-write]

Range of dates between which dates are selectable. For example, a date between 04-12-2006 and 04-12-2007 is selectable, but dates out of this range are disabled.

This property accepts an Object as a parameter. The Object contains two properties, rangeStart and rangeEnd, of type Date. If you specify only rangeStart, all the dates after the specified date are enabled. If you only specify rangeEnd, all the dates before the specified date are enabled. To enable only a single day in a DateChooser control, you can pass a Date object directly. Time values are zeroed out from the Date object if they are present.

The following example enables only the range January 1, 2006 through June 30, 2006. Months before January and after June do not appear in the DateChooser.

selectableRange="{{rangeStart : new Date(2006,0,1),
      rangeEnd : new Date(2006,5,30)}}"

The default value is null.

So in your case:

<mx:DateField id="myDateField" disabledRanges="{[ {rangeEnd: new Date()} ]}" />
CubicleGuyAuthor Commented:
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).

zzynxSoftware engineerCommented:
thanx 4 axxepting

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now