Primeng 日历:从数据库中设置日期 [英] Primeng calendar: Set date from database
问题描述
我正在使用 Primeng 日历,但我无法从数据库中设置日期.这是我在服务器上的日期:2018-04-17T16:41:47.683
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
当我尝试将格式更改为YYYY.MM.DD HH.MM"时,我在控制台上收到此错误:Uncaught (in promise): Unexpected literal at position 2当我将日期时间转换为字符串时,我得到同样的错误,我不知道该怎么做.请帮助
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
这是我的代码:
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:我用 ngmodel 尝试了两种方法.
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();
}
}
内容翻译:
export class ContentTranslate {
public StartDate: Date;
public FinishDate: Date;
public CreateDate: Date;
}
推荐答案
您需要为 ngModel 变量分配一个日期,而不是一个字符串值:
You need to assign a date, not a string, value to your ngModel variable:
this.datex = new Date(this.contentTranslate.StartDate);
我遇到了同样的问题,并得到了与您完全相同的错误,并通过将字符串转换为 JavaScript 日期来解决它.
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.
这篇关于Primeng 日历:从数据库中设置日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!