Setting format and value in input type=“date”

后端 未结 12 1558
暖寄归人
暖寄归人 2020-12-06 17:05

Is there any way to set the format of ? if no then how can i set date in this field using JavaScript in the default format of

相关标签:
12条回答
  • 2020-12-06 17:16

    @cOnstructOr provided a great idea, but it left a comma in place

    var today = new Date().toLocaleString('en-GB').split(' ')[0].slice(0,-1).split('/').reverse().join('-');
    

    fixes that

    0 讨论(0)
  • 2020-12-06 17:18
    function getDefaultDate(curDate){
    var dt = new Date(curDate);`enter code here`
    var date = dt.getDate();
    var month = dt.getMonth();
    var year = dt.getFullYear();
    if (month.toString().length == 1) {
        month = "0" + month
    }
    if (date.toString().length == 1) {
        date = "0" + date
    }
    return year.toString() + "-" + month.toString() + "-" + date.toString();
    }
    

    In function pass your date string.

    0 讨论(0)
  • 2020-12-06 17:24

    new Date().toISOString().split('T')[0];

    0 讨论(0)
  • 2020-12-06 17:25

    What you want to do is fetch the value from the input and assign it to a new Date instance.

    let date = document.getElementById('dateInput');
    
    let formattedDate = new Date(date.value);
    
    console.log(formattedDate);
    
    0 讨论(0)
  • 2020-12-06 17:26

    Easier than the above is

    var today = new Date().toISOString().substring(0,10); # "2013-12-31"
    
    0 讨论(0)
  • 2020-12-06 17:27

    The format is YYYY-MM-DD. You cannot change it.

    $('#myinput').val('2013-12-31'); sets value

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