Vlad Bezden Vlad Bezden - 1 month ago 9
HTML Question

How to set datetime on datetime-local via jQuery

I have datetime-local html control on my form and I need to set date and time on it dynamically via JS or jQuery. How can I do it?

<input type="datetime-local" id="publishDate" required/>


I tried

$("#publishDate").val("2013-3-18 13:00");
$("#publishDate").val("2013-3-18T13:00");
$("#publishDate").val(new Date().toLocalString());


but nothing worked.

Thanks in advance.

Answer

This would do the trick

$("#publishDate").val("2013-03-18T13:00");

You need to use 2 digits for the month to make your sample work.