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(new Date().toLocalString());

but nothing worked.

Thanks in advance.


This would do the trick


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

  • Thanks a lot. That worked. – Vlad Bezden Mar 18 '13 at 20:24
  • Thanks, but this is not working in firefox – shiva Jan 19 '15 at 8:55

If you want to set the current date, then you can try this:

__Method 1:__

    $('input[type=datetime-local]').val(new Date().toJSON().slice(0,19));

__Method 2:__

function zeroPadded(val) {
  if (val >= 10)
    return val;
    return '0' + val;

  d = new Date();
  $('input[type=datetime-local]').val(d.getFullYear()+"-"+zeroPadded(d.getMonth() + 1)+"-"+zeroPadded(d.getDate())+"T"+d.getHours()+":"+d.getMinutes()+":"+d.getSeconds());

Note: You can replace $('input[type=datetime-local]') with Id or Name or Class of the datetime-local field.

EDIT: d.getMonth() returns a value between 0-11, so to input the proper month 1 needs to be added to the result.

  • It's not working because all javascript date's methods returns a value without the a leading zero. stackoverflow.com/a/3605248/792624 – jan267 May 6 '14 at 7:29
  • Edited. Try and let me know :) – Varun Natraaj May 11 '14 at 8:23
  • 4
    Note new Date().toJSON().slice(0,19) and new Date().toISOString().slice(0,19) both returns UTC-time, not local time. – Hasse Björk Dec 5 '15 at 22:31
  • 1
    I've modified the script slightly, you need to use the zeroPadded function on the hours minutes and seconds else you'll get an error. $('input[type=datetime-local]').val(d.getFullYear()+"-"+zeroPadded(d.getMonth() + 1)+"-"+zeroPadded(d.getDate())+"T"+zeroPadded(d.getHours())+":"+zeroPadded(d.getMinutes())+":"+zeroPadded(d.getSeconds())); – James Sep 8 '16 at 10:05
  • I've edited to include the zero padding that @James points out – lucasvw Aug 30 '18 at 21:07

What about?

var now=new Date();
console.log(new Date(now.getTime()-now.getTimezoneOffset()*60000).toISOString().substring(0,19));


I wrote a jQuery method that sets the current UTC time. It's useful for initializing datetime and datetime-local input fields.

Here's how you would use it to initialize a field.


Or pass an argument of true to only set fields with no value.


Here's a solution in formatting the date using momentjs.
This will get the current date and time


This component is messed up because it's not specified properly yet. Implementations are likely to be quirky as well.

The right way to do it should be to pass a date object, with JS and DOM it would make no sense to not have this. Doing things with string manipulation is going to invoke Zalgo. Sooner or later it will break with the locale or timezone.

I have looked for something like this and in Chrome 46 found:

$('input[type=datetime-local]').prop('valueAsNumber', Math.floor(new Date() / 60000) * 60000); // 60seconds * 1000milliseconds

If you don't remove the second and milliseconds they will show in the input field.

There is a valueAsDate property as well but mysteriously:

Uncaught DOMException: Failed to set the 'valueAsDate' property on 'HTMLInputElement': This input element does not support Date values.

So they haven't finished implementing it yet or choose a bad name for that property (it shows as null even when something is set though).

Your Answer

