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 );
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, 2022Comments
-
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 over 7 yearsthank 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 over 7 yearsthank for your code. it works for me. I have another issue can u help me to solve it?
-
Anupa Sankar over 7 yearsChangeyear and changeMonth are not working for me. Do u know how can i solve this?
-
Prashant Kankhara over 7 yearsok. 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 over 7 yearsi have another issue. Changeyear and changeMonth are not working for me. Do u know how can i solve this?
-
Anupa Sankar over 7 yearsi got answer in fiddle but i don't get answer in my webpage.
-
Prashant Kankhara over 7 yearsIn 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 over 7 yearssorry i don't have permission to share that.
-
Prashant Kankhara over 7 yearsCheck in your code, do you have any other jquery plugin which has similar features ?
-
Anupa Sankar over 7 yearsjsfiddle.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 over 7 yearsthen 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 over 7 yearswhich jquery /.js scripts are you using?, use latest ones. That might be the reason why you are not getting the entire functionality.
-
AKhil N over 7 yearsuse jquery-1.9.1.js
-
Anupa Sankar over 7 yearsi am using jquery-1.9.1.js
-
Anupa Sankar over 7 yearsi don't get drop-down for year and month. i cleared browsing & application cache,but still the problem is there
-
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 over 7 yearssorry something went wrong then, I didn't find any problem in there.
-
Prashant Kankhara over 7 yearsget the latest jquery.ui and set it in your project and chek
-
Anupa Sankar over 7 yearsin page inspect i got drop-down for month and year. but when i try to run the website it is not working.