首页 > 解决方案 > 无法在输入类型日期中设置日期

问题描述

我知道,以前有人问过类似的问题,但我不明白答案/我觉得它不能解决我的问题。我无法在我的输入类型日期中设置日期。

export default function NewNoteModal() {
    return(
        <div id = "newNoteModal">
            {minimumSetDate()}
            <div id = "newNote-header">
                <h2>NoteKeeper</h2>
            </div>

            <form id = "newNote-content">
                <label htmlFor = "noteName">Note Name:</label>
                <input type="text" id = "noteName"></input>
                <br></br>
                {/* ---------------------- */}
                <label htmlFor="checkB-important">Important:</label>
                <input type="checkbox" name="important" value="important" id = "checkB-important"></input>
                <br></br>
                {/* ---------------------- */}
                <label htmlFor="remindDate">Remind me on:</label>
                <input type="date" name="remindDate" id = "remindDate"></input>
                <br></br>
                {/* ---------------------- */}
            </form>
        </div>
    );
}

function minimumSetDate(){
    let today = new Date();

    let day = today.getDate(); // typeof = number
    let month = ('0' + (today.getMonth()+1)).slice(-2); // typeof = number
    let year = today.getFullYear(); // typeof = number

    let currentDate = `${year}-${month}-${day}`; // typeof = string;
    console.log(typeof(currentDate), currentDate);
    document.getElementById("remindDate").value = currentDate;
}

控制台给出“TypeError:无法设置属性'value' of null”;

但是,这在 w3schools 中有效

document.getElementById("myDate").value = "2014-02-09";

标签: javascripthtmlreactjs

解决方案


let today = new Date();

let day = today.getDate(); // typeof = number
let month = ('0' + (today.getMonth()+1)).slice(-2) // typeof = number
let year = today.getFullYear(); // typeof = number

let currentDate = `${year}-${month}-${day}`; // typeof = string;
document.getElementById("remindDate").value = currentDate;
<input type="date" id="remindDate">


推荐阅读