最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin3浏览0评论

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/

发布评论

评论列表(0)

  1. 暂无评论