简体   繁体   English

Angular 4输入类型日期格式

[英]Angular 4 input type date format

在项目编辑选项中,我有一个这样的表格。如何将日期格式更改为dd / MM / yy。例如07/07/18但反映为整年。如何覆盖此格式?

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

Set the value returned by the below fiddler function to your model and bind it to your view. 将下面的提琴手函数返回的值设置为模型,并将其绑定到视图。 jsfiddle 的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));

You should use <input type="text" and Regex to format your text input to a date format you want. 您应该使用<input type="text"和Regex将文本输入格式化为所需的日期格式。 More on regex here and here 这里这里更多关于正则表达式的信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 angular 6 反应形式和输入类型日期格式 - angular 6 reactive form and input type date format 无法使用输入类型日期更改 angular 中的输入日期格式 - Unable to change input date format in angular using input type date 如何在 angular 4 中更改 input type=&quot;date&quot; 的输出日期格式? - How to change the output date format of input type=“date” in angular 4? 在 angular 输入/日期选择器中输入格式日期 - format date on type in angular input/date-picker Angular 和输入类型 Date 不会显示以 T 格式分配的日期 - Angular and input type Date won't display date assigned in format with T 当我使用angular 2并正式使用时,有什么方法可以更改输入type =“ date”格式吗? - when i use angular 2 and with formly Is there any way to change input type=“date” format? Angular 7 (Change)-Function on Input type Date 不起作用,但为什么格式错误? - Angular 7 (Change)-Function on Input type Date does not work, but why is the format wrong? 在Angular2 rc4中将自定义ControlValueAccessor用于输入类型=日期时出现格式错误 - Format error when using a custom ControlValueAccessor for an input type=date in Angular2 rc4 在 Angular2+ 中设置日期的输入格式 - Set input format of date in Angular2+ 更改日期输入字段格式的角度 - Change Date input field format in angular
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM