Primeng calendar: Set date from database
Asked Answered
U

1

2

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![1]

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;
}
Underlayer answered 26/4, 2018 at 8:52 Comment(0)
F
6

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.

enter image description here

Frostwork answered 17/6, 2018 at 21:33 Comment(1)
When I convert the value I received from the calendar to Date object, I am getting one day late. How this can be fixed ?Tranquillize

© 2022 - 2024 — McMap. All rights reserved.