DatePicker With Today and DateFormat

hawkhorrow picture hawkhorrow · May 3, 2013 · Viewed 10.2k times · Source

I have been trying to create a jQuery UI function for the datepicker, and after reading all the forums and snippets out there, I realize that a vast majority of answers have been deprecated and/or removed from jQuery 1.9.1.

Very Simply, I need to have the jquery datepicker

Display the datepicker with the options of buttons as well as be able to click on today which will automatically enter the date into my input field. For the life of me I can't figure this out and wanted to toss it to someone more knowledgeable in jQuery/ jQuery-UI.

Had Tried Something Like:

$(function() {
    $( "#date" ).datepicker(("setDate", new Date()){
        showButtonPanel: true,
        dateFormat: 'm-d-yy'
    });
  });

And of course the html

  <input name="date" id="date" />

The inputed value should be in standard USA date format: i.e. 5/1/2013 or 12/31/2099.

As of now, the code doesn't select the current date, and obviously clicking today will only select today's date. I need it to automatically populate the input field. Thank you in advance for your help!

Answer

Thanos picture Thanos · May 3, 2013

I've created an EXAMPLE 1 for you that think would cover what you ask.

EDIT: Updated the EXAMPLE 2 and removed the initial date selection. Now it starts without any selected date.

HTML

<p>Date: <input type="text" id="datepicker" /></p>

JS (don't forget yo include jQuery UI 1.9.2 and jQuery )

var $datepicker = $("#datepicker");
$datepicker.datepicker({
        showButtonPanel: true,
        dateFormat: 'm-d-yy'
    });
$datepicker.datepicker('setDate');