How to set default date value as current date in a datepicker textbox?

34,070

Solution 1

This may work for you,try it :

var lastDate = new Date();
lastDate.setDate(lastDate.getDate() - 7);//any date you want
$("#datepickerFrom").datepicker('setDate', lastDate);

Solution 2

Have you tried this ?

 var currentDate = new Date();
 $("#datepickerTo").datepicker({ defaultDate:  currentDate});

Solution 3

function getLastWeek(_date){
    var today = new Date(_date);
    var lastWeek = new Date(today.getFullYear(), today.getMonth(), today.getDate() - 7);
    return lastWeek ;
}

var lastWeekDate = $.datepicker.formatDate('mm/dd/yyyy', getLastWeek('20/11/2015'));
 $("#dateTimePicker").datepicker( "setDate" , laswWeekDate );
Share:
34,070
Anupa Sankar
Author by

Anupa Sankar

By day: I am a student. By night: Working on creating my own website like to do programming. interested in asp.net.

Updated on July 09, 2022

Comments

  • Anupa Sankar
    Anupa Sankar almost 2 years

    I have two datepicker. I wanted to set today's date as default in one datepicker, and 7 day before in another datepicker. Also I want drop-down list for selecting month and year in datepicker.

    in script I wrote the code below

     $("#datepickerTo").datepicker({
                     dateFormat: 'mm-dd-yy', 
                     changeMonth: true,
                     changeYear: true,
                     yearRange: '-70:+10',
    
    
    });
    
    $("#datepickerFrom").datepicker({
                     dateFormat: 'mm-dd-yy', 
                     changeMonth: true,
                     changeYear: true,
                     yearRange: '-70:+10',
    
    
    });
    

    but it is not working for me. please help me to solve this.

    i also tried setDate, new Date()

  • Anupa Sankar
    Anupa Sankar over 7 years
    thank u for your code . I got values in textbox. but i need a date that is one week before current date. now i am getting value that is one week after current date.
  • Anupa Sankar
    Anupa Sankar over 7 years
    thank for your code. it works for me. I have another issue can u help me to solve it?
  • Anupa Sankar
    Anupa Sankar over 7 years
    Changeyear and changeMonth are not working for me. Do u know how can i solve this?
  • Prashant Kankhara
    Prashant Kankhara over 7 years
    ok. You have to modify the last line. to decrease the 7 days $("#datepickerTo").datepicker("setDate" , "selected date of datepickerFrom - 7"); to with the solution from @AkhilN
  • Anupa Sankar
    Anupa Sankar over 7 years
    i have another issue. Changeyear and changeMonth are not working for me. Do u know how can i solve this?
  • Anupa Sankar
    Anupa Sankar over 7 years
    i got answer in fiddle but i don't get answer in my webpage.
  • Prashant Kankhara
    Prashant Kankhara over 7 years
    In that case there must be some confliction in your webpage. is it live ? if yes share that url. else create an html and share it.
  • Anupa Sankar
    Anupa Sankar over 7 years
    sorry i don't have permission to share that.
  • Prashant Kankhara
    Prashant Kankhara over 7 years
    Check in your code, do you have any other jquery plugin which has similar features ?
  • Anupa Sankar
    Anupa Sankar over 7 years
    jsfiddle.net/f2md3zy5/225 check this. i wanted to get it like this. but when i am using the same code in my website i don't get the drop-down for month and year.
  • Prashant Kankhara
    Prashant Kankhara over 7 years
    then its should work. What exactly you are getting when the script is fully loaded? No dropdowns for the year/month? Can you check by clearing the browser & application cache ?
  • AKhil N
    AKhil N over 7 years
    which jquery /.js scripts are you using?, use latest ones. That might be the reason why you are not getting the entire functionality.
  • AKhil N
    AKhil N over 7 years
    use jquery-1.9.1.js
  • Anupa Sankar
    Anupa Sankar over 7 years
    i am using jquery-1.9.1.js
  • Anupa Sankar
    Anupa Sankar over 7 years
    i don't get drop-down for year and month. i cleared browsing & application cache,but still the problem is there
  • AKhil N
    AKhil N over 7 years
    <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"‌​> <link rel="stylesheet" href="/resources/demos/style.css"> <script src="code.jquery.com/jquery-1.12.4.js"></script> <script src="code.jquery.com/ui/1.12.1/jquery-ui.js"></script> use these may help you
  • AKhil N
    AKhil N over 7 years
    sorry something went wrong then, I didn't find any problem in there.
  • Prashant Kankhara
    Prashant Kankhara over 7 years
    get the latest jquery.ui and set it in your project and chek
  • Anupa Sankar
    Anupa Sankar over 7 years
    in page inspect i got drop-down for month and year. but when i try to run the website it is not working.