How to set input type date's default value to today?

后端 未结 30 3485
刺人心
刺人心 2020-11-22 14:00

The HTML5 input types are great, Opera\'s new built-in date picker is a breeze, and Chrome has at least supported the new input type with a spin-wheel implementation.

<
30条回答
  •  面向向阳花
    2020-11-22 14:26

    You could fill the default value through javascript as seen here: http://jsfiddle.net/7LXPq/

    $(document).ready( function() {
        var now = new Date();
        var month = (now.getMonth() + 1);               
        var day = now.getDate();
        if (month < 10) 
            month = "0" + month;
        if (day < 10) 
            day = "0" + day;
        var today = now.getFullYear() + '-' + month + '-' + day;
        $('#datePicker').val(today);
    });
    

    I would probably put a bit of extra time to see if the month and date are single digits and prefix them with the extra zero...but this should give you an idea.

    EDIT: Added check for the extra zero

提交回复
热议问题