我正在一个项目中使用 Angular 和 Material2。
我以这种方式创建了一个 moment 对象:
myDate = moment.utc(new Date()).format("YYYY-MM-DD HH:mm:ss")
;
然后这个对象作为参数传递给需要知道格式化日期字符串的不同函数。
记录这个对象,我看到它有一个属性:_f: YYYY-MM-DD HH:mm:ss"
和另一个属性:_i: "2018-01-17 13:51:54"
有没有办法获取 _f
或 _i
的值?
编辑:
最后,我想要得到 "2018-01-17 13:51:54"
而不是真正的 _f
或 _i
。
编辑2:
myDate 充当该函数内的 Moment 对象。即:我正在扩展 MomentDateAdapter 并覆盖 format(date: Moment, displayFormat: string): string {}
所以 date 是一个 Moment 对象。这是在 Material2 Datepicker 内部使用的。
EDIT3 - 一些结果:
toString: 2018 年 1 月 17 日星期三 14:10:53 GMT+0100
格式:2018-01-17T14:10:53+01:00
toISOString:2018-01-17T13:10:53.000Z
最佳答案
作为Internal Properties您可以使用的指南状态:
To print out the value of a Moment, use
.format()
,.toString()
or.toISOString()
.
而不是_i
,而您可以使用creationData()
返回的format
属性而不是 _f
。
编辑后(最后,我想要得到“2018-01-17 13:51:54”,而不是真正的 _f
或 _i
),您可以简单地使用 format()
let myDate = moment.utc('2018-01-17 13:51:54'); // myDate is a moment object
let cData = myDate.creationData();
console.log( myDate.format("YYYY-MM-DD HH:mm:ss") );
console.log( cData.format );
console.log( myDate.format(cData.format) );
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.20.1/moment.min.js"></script>
关于javascript - Moment.js - 从对象获取显示格式或格式化值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48302962/