Primeng calendar: Set date from database

馋奶兔 提交于 2019-12-24 08:38:24

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!