PrimeNG <p-calendar>错误date.getMonth不是函数

时间:2018-04-03 14:45:37

标签: angular typescript primeng

我收到错误

  

core.es5.js:1020 ERROR错误:未捕获(在承诺中):TypeError:date.getMonth不是函数

     

TypeError:date.getMonth不是函数

每当我从后端接收JSON对象时尝试使用双向数据绑定时,我都会收到此错误。我的后端数据类型是日期,但它不会在p-calendar输入中显示备份。请帮忙!

{
"_id" : ObjectId("5a95cef390bd8fbf1c699d6d"),
"ar_asin" : "0078764343",
"ar_description" : "Brand new sealed!",
"ar_price" : 37.98,
"ar_imUrl" : "http://ecx.images-amazon.com/images/I/513h6dPbwLL._SY300_.jpg",
"ar_related" : {
    "also_bought" : [ 
        "B000TI836G", 
        "B003Q53VZC", 
        "B00EFFW0HC", 
        "B003VWGBC0", 
        "B003O6G5TW", 
        "B0037LTTRO", 
        "B002I098JE", 
        "B008OQTS0U", 
        "B005EVEODY", 
        "B008B3AVNE", 
        "B000PE0HBS", 
        "B00354NAYG", 
        "B0050SYPV2", 
        "B00503E8S2", 
        "B0050SY77E", 
        "B0022TNO7S", 
        "B0056WJA30", 
        "B0023CBY4E", 
        "B002SRSQ72", 
        "B005EZ5GQY", 
        "B004XACA60", 
        "B00273Z9WM", 
        "B004HX1QFY", 
        "B002I0K50U"
    ],
    "bought_together" : [ 
        "B002I098JE"
    ],
    "buy_after_viewing" : [ 
        "B0050SY5BM", 
        "B000TI836G", 
        "B0037LTTRO", 
        "B002I098JE"
    ]
},
"ar_salesRank" : {
    "Video Games" : 28655
},
"ar_categories" : [ 
    [ 
        "Video Games", 
        "Xbox 360", 
        "Games"
    ]
]
}

HTML

{ "applicationId": 1, "entryDate": 1524665731000, "ongoingDescription": "<p>Ongoing entry should end in (5081421)</p>", "activeFlag": "Y" }

TS

<div class="form-group">
  <label>Date of the Ongoing Incident</label>
  <br>
  <p-calendar required [(ngModel)]="entry.entryDate" name="entryDate" #entryDate="ngModel" [showIcon]="true" [showTime]="true" dateFormat="mm/dd/y 'EST'" hourFormat="24"></p-calendar>
  <div class="alert alert-danger" *ngIf="entryDate.touched && !entryDate.valid" >The date and time of the incident are required</div>
</div>

任何信息都有帮助

我试着也分配了      entryDate = new Date();和entryDate = new Date(this.entryDate);

1 个答案:

答案 0 :(得分:3)

它无法自动从JSON转换为日期,您必须自己分配。

this.id = this.route.snapshot.paramMap.get('id');
if (this.id) {
    this.service.get(this.id).take(1)
        .subscribe(service => this.entry = { entryDate: new Date(service.entryDate) });
}

永远不会分配或使用您的变量entryDate。您可以删除它,除非您想要引用p-calendar组件并且可以访问它的属性和方法。在这种情况下,您必须使用以下属性来装饰它:

@ViewChild('entryDate')
entryDate: Calendar;

请注意,类型是日历,而不是日期。它是一种primeng类型,应该从primeng

导入