【发布时间】:2019-02-21 09:32:58
【问题描述】:
我有一个 dateOfBirth 对象,我用时刻 js 创建了一个今天日期的对象 但是当我比较它们时,即使输入的日期小于今天的日期,它也会给我一个错误
这是我的 html 文件
<div class="form-group datepicker">
<label for="dob">Date of Birth*</label>
<div class="row input-group">
<input
ngbDatepicker
#d="ngbDatepicker"
#dobF="ngModel"
class="form-control input-underline input-lg"
id="dob"
[(ngModel)]="dateOfBirth"
placeholder="yyyy-mm-dd"
name="dp"
[ngClass]="{
invalid:
(dobF.value === null || isString(dobF.value) || (dateOfBirth.year > dobYear || dateOfBirth.month > dobMonth || dateOfBirth.day > dobDay) ) && dobF.touched
}"
required
/>
<div class="input-group-append">
<button
class="btn btn-outline-secondary calendar"
(click)="d.toggle()"
type="button"
></button>
</div>
</div>
<div
*ngIf="
(dobF.value === null || isString(dobF.value) || dateOfBirth.year > dobYear || dateOfBirth.month > dobMonth || dateOfBirth.day > dobDay ) && dobF.touched
"
class="error"
>
Please enter a valid date of birth.
</div>
</div>
这是我的 ts 文件,我在其中定义了我的 dob 代码
public dateOfBirth: { year: number; month: number; day: number };
public currentDate = moment().format("YYYY-MM-DD");
public dobYear: any;
public dobMonth: any;
public dobDay: any;
let obj = this.currentDate.split("-");
let obj2 = obj.map(Number);
this.dobYear = obj2[0];
this.dobMonth = obj2[1];
this.dobDay = obj2[2];
它给了我一个错误,因为今天的月份是 02,所以当我输入 2012-09-09 它给我一个错误,因为 02
【问题讨论】:
标签: javascript angular typescript date