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
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
Share Improve this question edited Feb 5, 2016 at 19:55 Alvaro Silvino 9,78314 gold badges55 silver badges81 bronze badges asked Feb 5, 2016 at 19:34 sqrepantssqrepants 1,1062 gold badges12 silver badges24 bronze badges 01 Answer
Reset to default 6When 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/alvarojoao/4hehnepw/