6

I am using a calender plug-in and I want to populate the current date into a text field. How can I do this using jquery?

Naveed
  • 41,517
  • 32
  • 98
  • 131
RPB
  • 16,006
  • 16
  • 55
  • 79

2 Answers2

12

You can get current date in JavaScript:

var now = new Date();

If you are using jQuery DatePicker you can apply it on any textfield like this:

$('input.youTextFieldClass').datepicker({ dateFormat: 'mm/dd/yy', 
                                     changeMonth: true,
                                     changeYear: true,
                                     yearRange: '-70:+10',
                                     constrainInput: false,
                                     duration: '',
                                     gotoCurrent: true});

If you want to set current date in textfields as page load:

$("input.youTextFieldClass").datepicker('setDate', new Date());
Community
  • 1
  • 1
Naveed
  • 41,517
  • 32
  • 98
  • 131
  • 3
    Throw a tag name on that selector and it will run faster. Replace `'.youTextFieldClass'` with `'input.youTextFieldClass'`. – chprpipr Jan 17 '11 at 05:13
2

You can find the sample here.

<input type="text" id="datepicker">
$( "#datepicker" ).datepicker({dateFormat:"dd M yy"}).datepicker("setDate",new Date());
Arun P Johny
  • 384,651
  • 66
  • 527
  • 531