【发布时间】:2020-01-21 11:27:00
【问题描述】:
我已经对需要根据本地日期在给定日期添加几个小时的组件做出反应。
render() {
let offset = new Date().getTimezoneOffset();
let localDate1=(moment(processItem.beginTime).add(-offset/60,'h'));
return(
<p>{localDate1}</p>/*need to display the 2019-09-18T16:02:00.00*/
)
}
这里的 processItem.beginTime 格式是 '2019-09-18T21:49:40+08:00'。当我在控制台登录时,'localDate1' it.给出以下内容:
{
_isAMomentObject: true,
_i: "2019-09-18T18:32:00+08:00",
_f: "YYYY-MM-DDTHH:mm:ssZ",
_tzm: 480,
_isUTC: false,
_pf: {…},
_locale: {…},
_d: Date 2019-09-18T16:02:00.000Z,
_isValid: true
}
正确的值是'_d: Date 2019-09-18T16:02:00.000Z',我想在我的代码中显示它。如何从对象中提取该值?
【问题讨论】:
-
使用momentjs的格式化功能momentjscom.readthedocs.io/en/latest/moment/04-displaying/…
-
您可以简单地使用
localDate1.toDate()。这是 moment 方法,它将从moment对象返回Date对象。您也可以使用localDate1.toISOString()这将在 ISO 中给出日期。