Javascript 如何设置现有 moment.js 对象的日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28982021/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set Date of an existing moment.js object
提问by Eduard Ghazanchyan
Let's say I have an existing moment object:
假设我有一个现有的时刻对象:
var m = moment(); // this will default to now
and want to update it with a new Date object, but WITHOUT REPLACING the entire object. E.g. this is NOT an acceptable solution for me:
并想用新的 Date 对象更新它,但不替换整个对象。例如,这对我来说不是一个可接受的解决方案:
m = moment(new Date());
The only solution I can find in there docs, is using set method:
我可以在那里的文档中找到的唯一解决方案是使用 set 方法:
m.set({'year': 2013, 'month': 3});
but in this way we'll need to split our existing Date object into peaces like this:
但通过这种方式,我们需要将我们现有的 Date 对象拆分成这样的和平:
var myDate = new Date();
var newDate = moment(myDate);
var splittedDate = {
year: newDate.get('year'),
month: newDate.get('month'),
date: newDate.get('date'),
hour: newDate.get('hour'),
minute: newDate.get('minute'),
second: newDate.get('second'),
millisecond: newDate.get('millisecond')
};
m.set(splittedDate);
But this looks ugly to me. Maybe someone can suggest a better solution?
但这对我来说看起来很丑陋。也许有人可以提出更好的解决方案?
回答by Arnaud
It may be a little late, but you can transform your new date to an object (newDate.toObject()) and pass it to the setmethod of your previous moment object:
可能有点晚了,但是您可以将新日期转换为对象 ( newDate.toObject()) 并将其传递给set上一个时刻对象的方法:
var m = moment(); // Initial moment object
// Create the new date
var myDate = new Date();
var newDate = moment(myDate);
// Inject it into the initial moment object
m.set(newDate.toObject());
回答by Andy Lorenz
- eg:1
moment("01/01/2000", "DD/MM/YYYY") - eg:2
moment("01 Jan 2000")
- 例如:1
moment("01/01/2000", "DD/MM/YYYY") - 例如:2
moment("01 Jan 2000")
for the OP, given a string formatted in a particular way, you simply pass in the string and its format to moment, such as:
对于 OP,给定以特定方式格式化的字符串,您只需将字符串及其格式传递给 moment,例如:
moment("01-01-2000 23:45", "DD-MM-YYYY HH:MM")
moment("01-01-2000 23:45", "DD-MM-YYYY HH:MM")
There are many options for the mask, these are all listed in the momentjs docs. You can even pass an array of possible masks and moment will try them all to see if it can make sense of your original string.
面具有很多选项,这些都列在 momentjs 文档中。您甚至可以传递一系列可能的掩码,并且 moment 会尝试所有这些,看看它是否可以理解您的原始字符串。
回答by Antoine
Just use your Dateobject as parameter:
只需使用您的Date对象作为参数:
var d = new Date();
var m = moment(d);
And voila, your moment object is set to the same time as your Date object.
而且瞧,你的时刻对象被设置为相同的时间,你的Date对象。
回答by Naved Ahmad
Its very simple, since the date object returned by javascript's inbuilt Date class is different as compared to the object that moment returns you can use
它非常简单,因为 javascript 的内置 Date 类返回的日期对象与您可以使用的 moment 返回的对象不同
const oldDate = new Date();
const newDate = moment(date);
this will return a date object from moment using the existing javascript's date object as the argument.
这将从使用现有 javascript 的日期对象作为参数的时刻返回一个日期对象。

