[英]Unable to set date in Angular material datepicker
I have a date which is fetched from the backed as 30-10-2019 (DD-MM-YYYY)
format.我有一个日期,它是从支持的30-10-2019 (DD-MM-YYYY)
格式中获取的。 I am trying to get this date to be displayed in angular material datepicker but its not displaying the date.我试图让这个日期显示在 angular 材料日期选择器中,但它不显示日期。
I tried moment and used moment (this.myDate).format('YYYY-MM-DD')
.toString();我尝试了 moment 并使用了 moment (this.myDate).format('YYYY-MM-DD')
.toString(); What am I missing?我错过了什么? Any other method (without using moment) is also fine.任何其他方法(不使用时刻)也可以。
try this尝试这个
this.date = new Date(this.myDate);
you need pass the date format to moment like below and assign that to your formControl or ngModel.您需要将日期格式传递给如下时刻,并将其分配给您的 formControl 或 ngModel。 Assuming your ngModel is date then code will be假设您的 ngModel 是日期,那么代码将是
this.date = moment(this.myDate, 'DD-MM-YYYY');
Here is the working example: https://stackblitz.com/edit/angular-w9szcc .这是工作示例: https://stackblitz.com/edit/angular-w9szcc 。
Hope this helps.希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.