【问题标题】:Converting DD-MM-YYYY to date format and setting value to html.Editor doesn't show value将 DD-MM-YYYY 转换为日期格式并将值设置为 html.Editor 不显示值
【发布时间】:2019-07-19 04:01:54
【问题描述】:

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

【问题讨论】:

    标签: javascript c# razor


    【解决方案1】:

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

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

    附言。您也可以查看 .toLocaleDateString 函数来格式化日期。

    【讨论】:

    • 使用 innerText 属性时出现的值。然而,Razor html.editor 样式将 vale 显示为 dd/mm/yyy 而不是传递给元素的日期。
    • @ANB 尝试使用myDate.innerText = dateFormatted.toString() ?
    猜你喜欢
    • 2023-03-23
    • 2017-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    • 2022-01-25
    相关资源
    最近更新 更多