我正在使用什么
- Angular Material 日期选择器
- Moment.js
- Firestore
我在做什么
当我创建新项目并将其添加到 firestore 时,我使用“new Date()”添加日期
这会创建火存储的以下格式:2017 年 10 月 13 日上午 9:00:04 UTC+1
这正是我想要的
Material 日期选择器可以很好地读取并显示13/10/2017
我想做什么
如果我编辑项目并从 Material 日期选择器中选择一个新日期并点击更新,我想转换日期选择器值,该值当前是一个字符串 18/12/2017强>(例如...)
在写回数据库之前,我希望该格式类似于 2017 年 12 月 18 日上午 9:00:04 UTC+1
问题
- 我该怎么做?
- 这是一个 moment.js 转换吗?
- 或者 Material 日期选择器是否有可以帮助我的选项?
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
}
最佳答案
您可以使用内置的日期格式化函数:
dateTest(newItemDate) {
var formatedDate = new Date(newItemDate);
// UTC Date
console.log(formatedDate.toUTCString());
// ISO Date
console.log(formatedDate.toISOString());
}
链接到StackBlitz Demo .
关于angular - 如何复制 'new Date()'格式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46726525/