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

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

    The simplest solutions seem to overlook that UTC time will be used, including highly up-voted ones. Below is a streamlined, ES6, non-jQuery version of a couple of existing answers:

    const today = (function() {
        const now = new Date();
        const month = (now.getMonth() + 1).toString().padStart(2, '0');
        const day = now.getDate().toString().padStart(2, '0');
        return `${now.getFullYear()}-${month}-${day}`;
    })();
    console.log(today); // as of posting this answer: 2019-01-24
    

提交回复
热议问题