将DD-MM-YYYY转换为日期格式并将值设置为html.Editor不显示值

时间:2019-07-19 04:01:54

标签: javascript c# razor

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

Razor HTML.Editor shows DD/MM/YYYY

1 个答案:

答案 0 :(得分:0)

document.querySelector();返回一个没有value属性的html对象。

尝试将myDate.value = dateFormatted更改为myDate.innerText = dateFormatted

PS。您也可以查看.toLocaleDateString函数来设置日期格式。