javascript - Onclick current date and time in datetime-local input - Stack Overflow

I am trying to get the current date and time on a datetime-local input field, using an onclick method.

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 0
Add a ment  | 

1 Answer 1

Reset to default 6

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/alvarojoao/4hehnepw/

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743958336a4536202.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信