DD-MM-YYYY中的字符串,如果转换为日期并传递给html.editor,则不会显示正确的值。
@Html.Editor("StartDate", new { htmlAttributes = new { @class = "form-control", @type = "date", @placeholder = " Start Date", @required = "", @id = "txtStrDate" } })
var myDate = document.querySelector(".update_ano #txtStrDate");
var Displaydate = convertDateToRequiredFormat(strdate); // Converts date to DD-MM-YYYY
var dateFormatted = new Date(Displaydate.replace(/(\d{2})-(\d{2})-(\d{4})/, "$2/$1/$3"));
console.log(dateFormatted)//Shows the correct date format
myDate.value = dateFormatted;
console.log(myDate.value);// Shows empty Console
答案 0 :(得分:0)
document.querySelector();
返回一个没有value属性的html对象。
尝试将myDate.value = dateFormatted
更改为myDate.innerText = dateFormatted
PS。您也可以查看.toLocaleDateString函数来设置日期格式。