首页 > 解决方案 > TypeError:date.getTime 在角度使用时不是函数

问题描述

我正在我的 Angular 7 应用程序中实现 kendo datetimepicker 控件并收到错误 TypeError: date.getTime is not a function

我绑定到 datetimepicker 之前的日期是 /Date(779929200000)/

我写了一个方法来把它转换成日期。所以我可以看到日期选择器,但可以在浏览器的控制台窗口上看到这个错误

用户界面

 <label for="inputFax" class="col-md-2  col-form-label header">Inception Date</label>
                <div class="col-md-3">
                    <div *ngIf="!EditMode">{{getInceptionDate}}</div>
                        <kendo-datepicker *ngIf="EditMode" [format]="'dd MMM, yyyy'"  [(ngModel)]="getInceptionDate" > </kendo-datepicker>
                </div>

零件

 get getInceptionDate(): string {
        if (this.FundDetails.Entity.INCEPTION_DATE != null) {
            const dateString = this.FundDetails.Entity.INCEPTION_DATE;
            const results = parseInt(dateString.replace(/\/Date\(([0-9]+)[^+]\//i, "$1"));
            const date = new Date(results);
            const month = date.toLocaleString('en-us', { month: 'long' });
            return (month + '-' + date.getFullYear());
        }
    }

标签: angularkendo-ui

解决方案


我认为您应该将您的重命名getInceptionDate为类似formatInceptionDate并创建另一个 getInceptionDate ,如下所示:

get getInceptionDate(): Date {
    if (this.FundDetails.Entity.INCEPTION_DATE != null) {
        return new Date(this.FundDetails.Entity.INCEPTION_DATE);
    }
}

推荐阅读