Results 1 to 9 of 9

Thread: How to set datepickerfield to default to today's date after opening it.

  1. #1
    Touch Premium Member
    Join Date
    Jun 2010
    Posts
    324

    Default How to set datepickerfield to default to today's date after opening it.

    I have a datepickerfield that by default should be null. However, if the user clicks to select the date, it currently defaults to 1/1/2000. I'd like to have the date default to today's date.

    How do I do this?

  2. #2

    Default

    Hi,

    You can set;

    Code:
    value : { day: new Date().getDate(), month: (new Date().getMonth()+1), year : new Date().getFullYear()}
    b.e.
    Code:
                  {
                    xtype: "datepickerfield",
                    name: "date",
                    label: null,
            id : "lastdate",
                    picker: { yearFrom: 2011, yearTo: 2012},
                    value : { day: new Date().getDate(), month: (new Date().getMonth()+1), year : new Date().getFullYear()}
                  }

  3. #3
    Touch Premium Member
    Join Date
    Jun 2010
    Posts
    324

    Default

    Thanks for the tip. Unfortunately I've already tried this. The date still defaults to 1/1/2011. I wonder if it is a bug.
    Quote Originally Posted by bassjobsen View Post
    Hi,

    You can set;

    Code:
    value : { day: new Date().getDate(), month: (new Date().getMonth()+1), year : new Date().getFullYear()}
    b.e.
    Code:
                  {
                    xtype: "datepickerfield",
                    name: "date",
                    label: null,
            id : "lastdate",
                    picker: { yearFrom: 2011, yearTo: 2012},
                    value : { day: new Date().getDate(), month: (new Date().getMonth()+1), year : new Date().getFullYear()}
                  }

  4. #4
    Touch Premium Member
    Join Date
    Jun 2010
    Posts
    324

    Default

    I take it back that worked. But I remember why I couldn't do it like this. The field should be blank by default. But when the user taps the field I'd like to default it to today's date. I'm looking for a tap event for the field or something, which I don't see in the docs.

  5. #5

    Default

    Well, okay
    There are more threads like this b.e. http://www.sencha.com/forum/showthre...-on-lost-focus

    You have to find the event which is fired before the select popup come up.
    So you can add a listener like:
    Code:
    listeners: 
    {
            idontkown : function()
        {
            this.setValue();
        }
    }
    select is fired after selecting a date so you need someting like beforeselect

  6. #6

    Default

    Try this:
    Code:
    {
                    xtype: "datepickerfield",
                    name: "date",
                    label: null,
            id : "lastdate",
                    picker: { yearFrom: 2011, yearTo: 2012},
                    listeners: {
                click: 
            {
                    element: 'el', 
                    fn: function()    { 
                               Ext.getCmp('lastdate').getDatePicker().setValue(new Date());//set today
                                       }
                }
    }

  7. #7
    Touch Premium Member
    Join Date
    Jun 2010
    Posts
    324

    Default

    You are genius! This worked perfectly. Thank you so much!
    Quote Originally Posted by bassjobsen View Post
    Try this:
    Code:
    {
                    xtype: "datepickerfield",
                    name: "date",
                    label: null,
            id : "lastdate",
                    picker: { yearFrom: 2011, yearTo: 2012},
                    listeners: {
                click: 
            {
                    element: 'el', 
                    fn: function()    { 
                               Ext.getCmp('lastdate').getDatePicker().setValue(new Date());//set today
                                       }
                }
    }

  8. #8
    Sencha User
    Join Date
    Sep 2014
    Posts
    23

    Default

    This does not work for me Would setting a placeholder override showing the default date?

  9. #9
    Sencha User
    Join Date
    Sep 2014
    Posts
    23

    Default

    Setting the value while defining the view seems to be broken. Instead you need to manually set the value after the element has loaded.

    Code:
    this.getDatePicker().setValue(new Date());
    Do this during the launch of your view, or if that does not work, fire an event after you've added the view to Ext.Viewport.
    Last edited by MrFox; 19 Jan 2015 at 5:03 AM. Reason: code example

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •