我用的是
我在做什么
我想做什么
问题
Component.html
<mat-form-field>
  <input #newItemDate #picker matInput [matDatepicker]="picker" placeholder="Choose a date" (focus)="picker.open()" formControlName="item_date">
  <mat-datepicker #picker></mat-datepicker>
</mat-form-field>
<button (click)="dateTest(newItemDate.value)"> Test Date </button> 
Component.ts
显然,这里不多:) --但这是我需要转换日期的地方
  dateTest(newItemDate) {    
    // newItemDate = 13/10/2017  
    // I need October 13, 2017 at 9:00:04 AM UTC+1
   
  }
发布于 2017-10-13 10:07:52
您可以使用内置日期格式函数:
 dateTest(newItemDate) {    
    var formatedDate = new Date(newItemDate);
    // UTC Date
    console.log(formatedDate.toUTCString());
    // ISO Date
    console.log(formatedDate.toISOString());
  }链接到StackBlitz Demo。
发布于 2017-10-13 09:40:03
您可以尝试从字符串中抽出一点时间来格式化它,如下所示:
// dateTest(newItemDate) {  
// This way you can make a moment right from the string :)
let newItemDate = moment('13/10/2017', 'DD/MM/YYYY').format("MMMM DD, YYYY H:MM:SS A Z");
console.log(newItemDate);
// }<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.min.js"></script>
编辑:修正了片段中缺少的moment.js导入。
https://stackoverflow.com/questions/46726525
复制相似问题