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

后端 未结 30 2979
刺人心
刺人心 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条回答
  • For NodeJS (Express with SWIG templates):

    <input type="date" id="aDate" name="aDate" class="form-control" value="{{ Date.now() | date("Y-m-d") }}" />
    
    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-11-22 14:27

    Follow the standard Y-m-d format, if you are using PHP

    <input type="date" value="<?php echo date("Y-m-d"); ?>">
    
    0 讨论(0)
  • 2020-11-22 14:27

    if you need to fill input datetime you can use this:

    <input type="datetime-local" name="datetime" 
           value="<?php echo date('Y-m-d').'T'.date('H:i'); ?>" />
    
    0 讨论(0)
  • 2020-11-22 14:29

    This is very much simple by applying following code, Using PHP

    <input type="date" value="<?= date('Y-m-d', time()); ?>" />
    

    Date function will return current date, by taking date in time().

    0 讨论(0)
  • 2020-11-22 14:30

    The following code works well:

    <input type="date" value="<?php echo date('Y-m-d'); ?>" />
    

    Note that this relies on PHP.

    0 讨论(0)
提交回复
热议问题