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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 01:30:30  来源:igfitidea点击:

How to get date without time in existing momentjs object?

javascriptdatemomentjs

提问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');

See http://momentjs.com/docs/#/manipulating/start-of/

http://momentjs.com/docs/#/manipulating/start-of/

回答by Josh Davenport

The momentjs object will always store a time, regardless of whether you use it. However, the following will clone dateto date2and reset the time:

momentjs 对象将始终存储时间,无论您是否使用它。但是,以下内容将克隆datedate2并重置时间:

var date2 = date.clone().hour(0).minute(0).second(0).millisecond(0)

You'll now have two independent momentjs objects dateand date2

您现在将拥有两个独立的 momentjs 对象datedate2