我该如何解决这个错误?天数:NaN 年NaN 月NaN 周NaN 天NaN 分钟NaN 秒
How can I fix this error ? days: NaN yearsNaN monthsNaN weeksNaN daysNaN minutesNaN seconds
我正在做一个离子项目,遇到这个错误。
我有一个按钮,我想要打印用户 select
的日期
我的代码
age.component.ts
import { Component, OnInit } from '@angular/core';
import * as moment from'moment';
@Component({
.....
})
export class AgeComponent implements OnInit {
dob:any = '' ;
myDate = {
years: 0,
months:0,
weeks:0,
days:0,
minutes:0,
seconds:0,
}
constructor() { }
calculateAge(){
let todaysDate = moment(new Date());
let dob = moment( new Date(this.dob) );
let duration = moment.duration(todaysDate.diff(dob));
this.myDate.years = duration.years();
this.myDate.months = Math.floor(duration.asMonths());
this.myDate.weeks = Math.floor(duration.asWeeks());
this.myDate.days = Math.floor(duration.asDays());
this.myDate.minutes = Math.floor(duration.asMinutes());
this.myDate.seconds = Math.floor(duration.asSeconds());
}
}
这是我的 age.component.html 代码
<ion-datetime class="datetime" [(ngModel)]="dob" pickerFormat="MMM DD YYYY"
displayFormat="MMM DD, YYYY" min="1950-01-01" max="2050-01-01"></ion-datetime>
<ion-button (click)="calculateAge()" >Calculate</ion-button>
<span>{{myDate.years}} years</span>
<span>{{myDate.months}} months</span>
<span>{{myDate.weeks}} weeks</span>
<span>{{myDate.days}} days</span>
<span>{{myDate.minutes}} minutes</span>
<span>{{myDate.seconds}} seconds</span>
NaN 错误只会在您不 select 来自 ion-datetime
的任何日期时发生。
要解决该问题,您只需将变量 let dob
中的条件添加到 let dob = moment( new Date(this.dob ? this.dob : todaysDate));
TS
calculateAge(){
let todaysDate = moment(new Date());
let dob = moment( new Date(this.dob ? this.dob : todaysDate)); <-- add condition here
let duration = moment.duration(todaysDate.diff(dob));
this.myDate.years = duration.years();
this.myDate.months = Math.floor(duration.asMonths());
this.myDate.weeks = Math.floor(duration.asWeeks());
this.myDate.days = Math.floor(duration.asDays());
this.myDate.minutes = Math.floor(duration.asMinutes());
this.myDate.seconds = Math.floor(duration.asSeconds());
}
这是sample
我正在做一个离子项目,遇到这个错误。 我有一个按钮,我想要打印用户 select
的日期我的代码
age.component.ts
import { Component, OnInit } from '@angular/core';
import * as moment from'moment';
@Component({
.....
})
export class AgeComponent implements OnInit {
dob:any = '' ;
myDate = {
years: 0,
months:0,
weeks:0,
days:0,
minutes:0,
seconds:0,
}
constructor() { }
calculateAge(){
let todaysDate = moment(new Date());
let dob = moment( new Date(this.dob) );
let duration = moment.duration(todaysDate.diff(dob));
this.myDate.years = duration.years();
this.myDate.months = Math.floor(duration.asMonths());
this.myDate.weeks = Math.floor(duration.asWeeks());
this.myDate.days = Math.floor(duration.asDays());
this.myDate.minutes = Math.floor(duration.asMinutes());
this.myDate.seconds = Math.floor(duration.asSeconds());
}
}
这是我的 age.component.html 代码
<ion-datetime class="datetime" [(ngModel)]="dob" pickerFormat="MMM DD YYYY"
displayFormat="MMM DD, YYYY" min="1950-01-01" max="2050-01-01"></ion-datetime>
<ion-button (click)="calculateAge()" >Calculate</ion-button>
<span>{{myDate.years}} years</span>
<span>{{myDate.months}} months</span>
<span>{{myDate.weeks}} weeks</span>
<span>{{myDate.days}} days</span>
<span>{{myDate.minutes}} minutes</span>
<span>{{myDate.seconds}} seconds</span>
NaN 错误只会在您不 select 来自 ion-datetime
的任何日期时发生。
要解决该问题,您只需将变量 let dob
中的条件添加到 let dob = moment( new Date(this.dob ? this.dob : todaysDate));
TS
calculateAge(){
let todaysDate = moment(new Date());
let dob = moment( new Date(this.dob ? this.dob : todaysDate)); <-- add condition here
let duration = moment.duration(todaysDate.diff(dob));
this.myDate.years = duration.years();
this.myDate.months = Math.floor(duration.asMonths());
this.myDate.weeks = Math.floor(duration.asWeeks());
this.myDate.days = Math.floor(duration.asDays());
this.myDate.minutes = Math.floor(duration.asMinutes());
this.myDate.seconds = Math.floor(duration.asSeconds());
}
这是sample