javascript 如何在现有的momentjs对象中获取没有时间的日期?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23758345/
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 get date without time in existing momentjs object?
提问by Erik
Let's say I have momentjs object like the following:
假设我有如下的 momentjs 对象:
var date = moment(new Date(2014,2,17,9,60));
How could I get clone and get new momentjs object without time?
如何在没有时间的情况下获得克隆并获得新的 momentjs 对象?
回答by yar0d
With moment version 1.7 and above, just use startOf method.
对于 moment 1.7 及以上版本,只需使用 startOf 方法。
var date2 = date1.clone().startOf('day');
回答by Josh Davenport
The momentjs object will always store a time, regardless of whether you use it. However, the following will clone date
to date2
and reset the time:
momentjs 对象将始终存储时间,无论您是否使用它。但是,以下内容将克隆date
到date2
并重置时间:
var date2 = date.clone().hour(0).minute(0).second(0).millisecond(0)
You'll now have two independent momentjs objects date
and date2
您现在将拥有两个独立的 momentjs 对象date
和date2