How to customize date in to 2017-11-20T11:23:00 - Angular material 5

前端 未结 2 747
情深已故
情深已故 2021-01-27 11:42

I am using following code for displaying date picker


  
           


        
2条回答
  •  粉色の甜心
    2021-01-27 11:59

    In your component.html, use two inputs, first one to hold date from mat-datepicker, second one bind with model. In (selectedChanged) event of the 'mat-datepicker' update 'myModel.MyDateString' from the date-picker value:

    
        
        
        
        
      
    

    Inside your component.ts:

    updateDateToString(event) {
    let newDate = new Date(event)
    
    let dd: number | string = newDate.getDate();
    if (dd < 10) {
      dd = '0' + dd;
    }
    let mm: number | string = newDate.getMonth() + 1;
    if (mm < 10) {
      mm = '0' + mm;
    }
    const yy: number = newDate.getFullYear();
    this.myModel.MyDateString = `${yy}-${mm}-${dd}`;    
    

    }

提交回复
热议问题