Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Onclick current date and time in datetime-local input

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

like image 496
sqrepants Avatar asked Feb 05 '16 19:02

sqrepants


1 Answers

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/

like image 100
Alvaro Joao Avatar answered Sep 24 '22 15:09

Alvaro Joao