Angular 4 输入类型日期格式

Angular 4 input type date format

在项目编辑选项中,我有一个像 this.how 的表格来将日期格式更改为 dd/MM/yy。例如 07/07/18 但它反映为完整 year.how 以覆盖此?

<input type="date"  class="form-control" name="published" #published [(ngModel)] = "books.published"  [ngModelOptions]="{ standalone: true }"/>

您应该使用 <input type="text" 和 Regex 将您的文本输入格式化为您想要的日期格式。有关正则表达式的更多信息 here and here

将以下 fiddler 函数返回的值设置为您的模型并将其绑定到您的视图。 jsfiddle

 //A function for formatting a date to MM/dd/yy
    function formatDate(d)
    {
        //get the month
        var month = d.getMonth();
        //get the day
        //convert day to string
        var day = d.getDate().toString();
        //get the year
        var year = d.getFullYear();

        //pull the last two digits of the year
        year = year.toString().substr(-2);

        //increment month by 1 since it is 0 indexed
        //converts month to a string
        month = (month + 1).toString();

        //if month is 1-9 pad right with a 0 for two digits
        if (month.length === 1)
        {
            month = "0" + month;
        }

        //if day is between 1-9 pad right with a 0 for two digits
        if (day.length === 1)
        {
            day = "0" + day;
        }

        //return the string "MMddyy"
        return month +'/'+ day +'/'+ year;
    }

    var d = new Date();
    alert(formatDate(d));