问题
I'm using primeng calendar but i can't set the date from database. This is my date from server: 2018-04-17T16:41:47.683
When i try to change format to "YYYY.MM.DD HH.MM" witht moment, i get this error on console: Uncaught (in promise): Unexpected literal at position 2 When i convert my datetime to string, i get same error and i don't know what to do. Please help
Here is my code:
Component.html
<p-calendar id="StartDate" inputStyleClass="form-control" [showIcon]="true" showTime="true" hourFormat="24" [locale]="tr"
name="StartDate" [(ngModel)]="datex"></p-calendar>
<!-- <p-calendar id="StartDate" inputStyleClass="form-control" [showIcon]="true" showTime="true" hourFormat="24" [locale]="tr"
name="StartDate" [(ngModel)]="contentTranslate.StartDate"></p-calendar> -->
ps: i tried both way with ngmodel.
Component.ts
ngOnInit() {
this.getLanguageDetail(this.contentId, this.langId); }
getLanguageDetail(contentId: number, langId: number): any {
this.contentService
.getTranslateDetail(contentId, langId)
.subscribe(x => this.detailResultFunc(x));
}
detailResultFunc(x: any): any {
if (x) {
this.contentTranslate = x;
const tmpDate: string = moment(this.contentTranslate.StartDate).format('YYYY.MM.DD HH:MM');
this.datex = tmpDate; new Date(tmpDate).toLocaleDateString(); //.toLocaleDateString().trim();
} else {
this.contentTranslate = new ContentTranslate();
}
}
ContentTranslate:
export class ContentTranslate {
public StartDate: Date;
public FinishDate: Date;
public CreateDate: Date;
}
回答1:
You need to assign a date, not a string, value to your ngModel variable:
this.datex = new Date(this.contentTranslate.StartDate);
I ran into the same issue and got the exact same error that you did and resolved it by converting the string into a JavaScript date.
来源:https://stackoverflow.com/questions/50038773/primeng-calendar-set-date-from-database