jQuery Datepicker  - display todays date on load

catonthecouchproductions
catonthecouchproductions used Ask the Experts™
on
I am using the jQuery UI Datepicker and am trying to do this method:

var d_date = new Date();
console.log(d_date);
$('.date').datepicker('setDate',d_date);

I want the input to be populated on page load within my input. I checked out the docs here:

http://jqueryui.com/demos/datepicker/#method-setDate

Doesnt seem to be working, but when I use console.log it displays the contents, the full JS date object, does it need to become a string and then format the date to mm/dd/yyyy

Thanks,

Ryan
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
If you want the input box to automatically be populated with todays date just set the value of the input directly
$("#InputID").val("7/1/2010")

I'm pretty sure setdate just sets the date that will start selected in the calendar, not update the textbox
JF0

Commented:
Try this:
$('.date').datepicker('setDate',+0);
Could I use the date object and have it do it to whatever the day is? using the .val()?

Ryan
@JF0 - when I use that it doesnt even show the datepicker or populate the field. Its weird, no errors too.

Ryan

Commented:
Yeah you can use the current date object, but if you pass it to the value it might not display in the format you want. Use the methods for the date object if you want to change how it appears in the input http://www.w3schools.com/jsref/jsref_obj_date.asp
Sorry kind of confused on working with this?

Would I use this?

Date.toDateString()

Not sure how to get the correct format: mm/dd/yyyy - I think the biggest thing is getting month day year all together, do you use all three objects and combine?

getDate()
getDay()
getFullYear()


var d = getDate();

FORMATTING HERE

$(".date").val(new_date)

Open in new window

Commented:
To get mm/dd/yyyy do the following:

var today = new Date()

$(".date").val(today.getMonth() + "/" + today.getDate() + "/" + today.getFullYear())
Oh.. it would that easy as just concatting. wow..let me try this.

Thank you,

Ryan
Perfect, it seems to be displaying 6 instead of 7 for the month of June.

Any ideas? Ill see if there is an offset in the docs.
Commented:
Oh yeah javascript months are 0 based (for whatever reason). So change your code to:

var today = new Date()
var month = today.getMonth() + 1;

$(".date").val(month + "/" + today.getDate() + "/" + today.getFullYear())
Thank you for all your help! Appreciate it.

Worked great.

Ryan

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial