set date in input type date

<input id="datePicker" type="date" />​

i will set today date in datepicker input type date in chrome.

$(document).ready( function() {
    var now = new Date();
    var today = now.getDate()  + '/' + (now.getMonth() + 1) + '/' + now.getFullYear();
    alert(today);
    $('#datePicker').val(today);
});​

but it's not working

Edit This jsFiddle - Please try in chrome.

Answers


Fiddle link : http://jsfiddle.net/7LXPq/93/

Two problems in this:

  1. Date control in HTML 5 accepts in the format of Year - month - day as we use in SQL
  2. If the month is 9, it needs to be set as 09 not 9 simply. So it applies for day field also.

Please follow the fiddle link for demo:

var now = new Date();

var day = ("0" + now.getDate()).slice(-2);
var month = ("0" + (now.getMonth() + 1)).slice(-2);

var today = now.getFullYear()+"-"+(month)+"-"+(day) ;

$('#datePicker').val(today);

Need Your Help

magento: add attibute to product, but not show up when editing the product

magento attributes magento-1.4 product

As per bens comment and answer I updated my script, comments indicate changes

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.