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

后端 未结 30 2996
刺人心
刺人心 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:31

    In HTML5 as such, there is no way to set the default value of the date field to today’s date? As shown in other answers, the value can be set using JavaScript, and this is usually the best approach if you wish to set the default according to what is current date to the user when the page is loaded.

    HTML5 defines the valueAsDate property for input type=date elements, and using it, you could set the initial value directly from an object created e.g. by new Date(). However, e.g. IE 10 does not know that property. (It also lacks genuine support to input type=date, but that’s a different issue.)

    So in practice you need to set the value property, and it must be in ISO 8601 conformant notation. Nowadays this can be done rather easily, since we can expect currenty used browsers to support the toISOString method:

    <input type=date id=e>
    <script>
    document.getElementById('e').value = new Date().toISOString().substring(0, 10);
    </script>
    
    0 讨论(0)
  • 2020-11-22 14:32

    This is what I did in my code, I have just tested and it worked fine, input type="date" does not support to set curdate automatically, so the way I used to overcome this limitation was using PHP code a simple code like this.

    <html>
    <head></head>
        <body>
            <form ...>
                <?php
                    echo "<label for='submission_date'>Data de submissão</label>";
                    echo "<input type='date' name='submission_date' min='2012-01-01' value='" . date('Y-m-d') . "' required/>";
                ?>
            </form>
        </body>
    </html>
    

    Hope it helps!

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

    Even after all these time, it might help someone. This is simple JS solution.

    JS

      let date = new Date();
      let today = date.toISOString().substr(0, 10);
      //console.log("Today: ", today);//test
      document.getElementById("form-container").innerHTML =
        '<input type="date" name="myDate" value="' + today + '" >';//inject field
    

    HTML

     <form id="form-container"></form>
    

    Similar solution works in Angular without any additional library to convert date format. For Angular (code is shortened due to common component code):

    //so in myComponent.ts 
    //Import.... @Component...etc...
    date: Date = new Date();
    today: String; //<- note String
    //more const ...
    export class MyComponent implements OnInit {
       //constructor, etc.... 
       ngOnInit() {
          this.today = this.date.toISOString().substr(0, 10);
       }
    }
    //so in component.html 
    <input type="date" [(ngModel)]="today"  />
    
    0 讨论(0)
  • 2020-11-22 14:33

    Both top answers are incorrect.

    A short one-liner that uses pure JavaScript, accounts for the local timezone and requires no extra functions to be defined:

    const element = document.getElementById('date-input');
    element.valueAsNumber = Date.now()-(new Date()).getTimezoneOffset()*60000;
    <input id='date-input' type='date'>

    This gets the current datetime in milliseconds (since epoch) and applies the timezone offset in milliseconds (minutes * 60k minutes per millisecond).

    You can set the date using element.valueAsDate but then you have an extra call to the Date() constructor.

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

    This is something you really need to do server-side as each user's local time format differs, not to mention each browser behaves different.

    Html Date inputs value should be in this format: yyyy-mm-dd otherwise it will not show a value.

    ASP CLASSIC , OR VBSCRIPT:

    current_year = DatePart("yyyy",date) 
    current_month = DatePart("m",date) 
    current_day = DatePart("d",date) 
    
    IF current_month < 10 THEN
    current_month = "0"&current_month
    END IF
    IF current_day < 10 THEN
    current_day = "0"&current_day
    END IF
    
    get_date = current_year&"-"&current_month&"-"&current_day
    Response.Write get_date
    

    Output of today's date : 2019-02-08

    Then in your html: <input type="date" value="<% =get_date %>"

    PHP

    just use this: <input type="date" value="<?= date("Y-m-d"); ?>">

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

    Javascript

    document.getElementById('date-field').value = new Date().toISOString().slice(0, 10);
    

    Jquery

    $('#date-field').val(new Date().toISOString().slice(0, 10));
    

    Another Option

    If you want to customize the date, month and year just do sum or sub as your wish

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