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

后端 未结 30 3281
刺人心
刺人心 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: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 =
        '';//inject field
    

    HTML

     

    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 
    
    

提交回复
热议问题