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

Posted on 2009-04-16
Last Modified: 2012-05-06
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.

Question by:CubicleGuy
    LVL 5

    Expert Comment

    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

    LVL 1

    Author Comment

    I was right - it was not intuitive.  

    Thanks - I'll work on this immediately and get back with you.
    LVL 37

    Accepted Solution

    >> 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
    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()} ]}" />
    LVL 1

    Author Comment

    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).

    LVL 37

    Expert Comment

    thanx 4 axxepting

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Suggested Solutions

    Title # Comments Views Activity
    flex 4.5 upload with java servlet 1 1,044
    FLex Calendar 3 530
    Button and button function on separate pages. Possible? 16 401
    Flex 3 trace output 13 718
    First things first - Preparation We need all the part for this install and it's much nicer to have them all on hand when you need them so here's what's required. Download Eclipse 3.5 32 bit (I like the Classic flavour) from here. (http://www.e…
    If you're not part of the solution, you're part of the problem.   Tips on how to secure IoT devices, even the dumbest ones, so they can't be used as part of a DDoS botnet.  Use PRTG Network Monitor as one of the building blocks, to detect unusual…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now