I am trying to get the current date and time on a datetime-local
input field, using an onclick
method.
I have tried using moment.js
and other libraries to do this, but for some reason, it cannot translate into the current datetime-local
field. What am I doing wrong?
I decided to go back to basics with jquery
alone and this is what I manged to get so far.
If you change the input type to text
on the input box, the script works fine, but if its changed to datetime-local, it doesn't work.
$(
function(){
$('#time').click(function(){
var time = new Date();
$('#time-holder').val(time.toDateString());
});
}
);
JsFiddle: jfiddle
When use input type datetime-local
.
The date format must be as: 'YYYY-MM-DDTHH:mm:ss'
e.g: "2014-11-16T15:25:33"
$(
function(){
$('#time').click(function(){
var time = moment().format('YYYY-MM-DDTHH:mm:ss');
$('#time-holder').val(time);
});
}
);
solution here:
http://jsfiddle.net/alvarojoao/4hehnepw/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With