本文介绍了用moment.js添加日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在约会中添加一天:
I am trying to add a day to my date:
let createdDate = moment(new Date()).utc().format();
let expirationDate = moment(createdDate).add(1, 'd');
console.log(expirationDate);
但是,这总是返回一个晦涩的对象 {_ i: 2017-12 -20T21:06:21 + 00:00,_f: YYYY-MM-DDTHH:mm:ss Z,_l:undefined,_isUTC:false,_a:Array(7),...}
However, this keeps returning an obscure object {_i: "2017-12-20T21:06:21+00:00", _f: "YYYY-MM-DDTHH:mm:ss Z", _l: undefined, _isUTC: false, _a: Array(7), …}
小提琴:
有人知道我在做什么错吗?
Does anyone know what I might be doing wrong?
推荐答案
您正在记录矩对象。正如指南所述:
You are logging a moment object. As the Internal Properties guide states:
let createdDate = moment(new Date()).utc().format();
let expirationDate = moment(createdDate).add(1, 'd');
console.log(expirationDate.format());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.20.1/moment.min.js"></script>
请注意,您可以使用(无需使用 new Date()
)或。
Please note that you can get the current date using moment()
(no need to use new Date()
) or moment.utc()
.
这篇关于用moment.js添加日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!