Javascript 如何以(';Y-MM-DD';)格式从日期选择器角度材质格式化日期?反应形式
我什么都试过了,但没有成功。在示例中,我不做反应形式,而是ng模型,这对我来说非常简单:Javascript 如何以(';Y-MM-DD';)格式从日期选择器角度材质格式化日期?反应形式,javascript,angular,angular-reactive-forms,Javascript,Angular,Angular Reactive Forms,我什么都试过了,但没有成功。在示例中,我不做反应形式,而是ng模型,这对我来说非常简单: deliveryDate: moment(this.startDate).format('Y-MM-DD') 但在当前的代码中,我不能改变任何东西。看看我的代码: export const MY_FORMATS = { parse: { dateInput: 'MM/DD', }, display: { dateInput: 'MM/DD', monthYearL
deliveryDate: moment(this.startDate).format('Y-MM-DD')
但在当前的代码中,我不能改变任何东西。看看我的代码:
export const MY_FORMATS = {
parse: {
dateInput: 'MM/DD',
},
display: {
dateInput: 'MM/DD',
monthYearLabel: 'MMM YYYY',
dateA11yLabel: 'LL',
monthYearA11yLabel: 'MMMM YYYY',
},
};
@Component({
selector: 'app-my-order',
templateUrl: './my-order.component.html',
styleUrls: ['./my-order.component.scss'],
providers: [
// `MomentDateAdapter` can be automatically provided by importing `MomentDateModule` in your
// application's root module. We provide it at the component level here, due to limitations of
// our example generation script.
{
provide: DateAdapter,
useClass: MomentDateAdapter,
deps: [MAT_DATE_LOCALE, MAT_MOMENT_DATE_ADAPTER_OPTIONS]
},
{provide: MAT_DATE_FORMATS, useValue: MY_FORMATS},
],
})
resultOfdate: any = MY_FORMATS;
this.getProducts();
this.form = this.fb.group({
deliveryDate: [moment(this.resultOfdate).format('2020-MM-DD'), Validators.required], //this is not work format is hard-code on 2020 because deliveryDate from form return current date like a new Date(). this.resultOfdata is
address: [null, [Validators.required, Validators.minLength(5)]],
phone: [null, [Validators.minLength(9),Validators.maxLength(19)]],
city: [null, [Validators.required, Validators.minLength(5)]],
data: this.fb.array([this.createContact()]),
note: [null]
});
和我的模板:
<mat-form-field>
<input formControlName="resultOfdate" matInput [matDatepicker]="resultOfdate" placeholder="Choose a date" >
<mat-datepicker-toggle matSuffix [for]="resultOfdate" ></mat-datepicker-toggle>
<mat-datepicker #resultOfdate></mat-datepicker>
</mat-form-field>
我想发送一个2019-10-10示例,您可能在错误的位置使用了矩.format 请这样尝试,在dateChange上调用您的方法,如下所示:
export const MY_FORMATS_ORG = {
parse: {
dateInput: 'MM/DD',
},
display: {
dateInput: 'MM/DD',
monthYearLabel: 'MMM YYYY',
dateA11yLabel: 'LL',
monthYearA11yLabel: 'MMMM YYYY',
},
};
@Component({
selector: 'datepicker-formats-example',
templateUrl: 'datepicker-formats-example.component.html',
styleUrls: ['datepicker-formats-example.component.css'],
providers: [
{provide: DateAdapter, useClass: MomentDateAdapter, deps: [MAT_DATE_LOCALE]},
{provide: MAT_DATE_FORMATS, useValue: MY_FORMATS_ORG},
],
})
export class DatepickerFormatsExampleComponent {
date = new FormControl(moment());
dateData: string;
public setDate(date: string) {
this.dateData = date ? moment(date).format('Y-MM-DD') : '';
}
}
请在此处查看:
此外,这:
deliveryDate: moment(this.startDate).format('Y-MM-DD')
可能是错误的,应该是这样的:
deliveryDate: string;
作业将在方法中完成
我希望这会有帮助。你想完成什么?我想更好地理解,你能改进这个问题吗?如果您可以添加stackblitz,这可能会很有帮助。我想从给定的选取器格式化,而不是从当前的选取器(Sun Dec 22 2019 00:00:00 GMT+0100到2019-22)-12@Pabo你能看到我的问题吗?!这也许能帮助我。我需要尝试thisdeliveryDate:null this.form=this.fb.group({deliveryDate:[this.dateData],//矩(this.startDate)。格式('Y-MM-DD')地址:[null,[Validators.required,Validators.minLength(5)],电话:[null,[Validators.minLength(9),Validators.maxLength(19)],城市:[null,[Validators.required,Validators.minLength(5)],数据:this.fb.array([this.createContact()]),注意:[null]});看看我的代码,我有点困惑,你能在stackblitz上做一个最小的函数示例吗?Codepen不会执行你的代码。我会在我的工作完成后立即检查。但我会的。