不在matdatepicker

时间:2018-01-30 07:41:12

标签: html angular angular-material2

当我没有在matdatepicker(angular2)中选择日期时,它将默认值设为" 01/01/207",而我需要传递' null&# 39;方法。我可以以某种方式更改默认值吗?

我使用的代码如下:

HTML:

<div class="div1">
   <form (ngSubmit)="getEventsByDateRange(f)" #f="ngForm" >
     <label style="float:left;padding:7px;"> From : </label>
      <mat-form-field style="float:left;padding:7px;">
        <input type="date" matInput [matDatepicker]="picker1" placeholder="Choose a date" name="fromDate" 
        [(ngModel)]="eventobj.fromDate" #eventDate="ngModel"  required>
        <mat-datepicker-toggle matSuffix [for]="picker1"></mat-datepicker-toggle>
        <mat-datepicker #picker1></mat-datepicker>
      </mat-form-field>
      <label style="float:left;padding:7px;"> To : </label>
      <mat-form-field style="float:left;padding:7px;">
        <input type="date" matInput [matDatepicker]="picker2" placeholder="Choose a date" name="toDate" 
        [(ngModel)]="eventobj.toDate" #eventDate="ngModel"  required>
        <mat-datepicker-toggle matSuffix [for]="picker2"></mat-datepicker-toggle>
        <mat-datepicker #picker2></mat-datepicker>
      </mat-form-field>
      <button mat-raised-button class="btn btn-info btn-sm"> Submit </button>
   </form>
</div>

TS:

eventobj = {
fromDate: "Date",
toDate: "Date",
}

 getJobsByDateRange() {
    let fromDate1=new Date(this.eventobj.fromDate)
    let toDate1=new Date(this.eventobj.toDate)

    alert("from :: "+fromDate1+" to :: "+toDate1)
 }

如果我直接点击提交按钮,则会显示警告消息:

  

from :: Thu Jan 01 1970 05:30:00 GMT + 0530(India Standard Time)to :: Thu Jan 01 1970 05:30:00 GMT + 0530(India Standard Time)

1 个答案:

答案 0 :(得分:0)

let fromDate1=new Date(this.eventobj.fromDate)
let toDate1=new Date(this.eventobj.toDate)

在这里,您声明字符串。但你使用日期......

无论如何,这意味着当你做

Date

您实际上将字符串(defaultDate = Date.now(); eventobj = { fromDate: new Date(this.defaultDate - 1000 * 60 * 60 * 24 * 7), // One week toDate: new Date(this.defaultDate), } getJobsByDateRange() { let fromDate1 = this.eventobj.fromDate; let toDate1 = this.eventobj.toDate; alert("from :: "+fromDate1+" to :: "+toDate1) } )转换为实际日期。你很幸运,你有一个约会对象!

用以下内容替换所有内容:

{{1}}