javascript 如何使用 Moment.js 从日期中删除时间?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15130735/
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-26 23:37:16  来源:igfitidea点击:

How can I remove time from date with Moment.js?

javascriptdatemomentjs

提问by Obsivus

formatCalendarDate = function (dateTime) {
    return moment.utc(dateTime).format('LLL');
};

It displays: "28 februari 2013 09:24"

它显示:“28 februari 2013 09:24”

But I would like to remove the time at the end. How can I do that?

但我想在最后删除时间。我怎样才能做到这一点?

I'm using Moment.js.

我正在使用Moment.js

回答by Graham Charles

Sorry to jump in so late, but if you want to removethe time portion of a moment()rather than formattingit, then the code is:

很抱歉这么晚才加入,但是如果您想删除 a的时间部分moment()而不是对其进行格式化,那么代码是:

.startOf('day')

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

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

回答by Joshua Pinter

Use format('LL')

利用 format('LL')

Depending on what you're trying to do with it, format('LL')could do the trick. It produces something like this:

取决于你想用它做什么,format('LL')可以做到这一点。它产生这样的东西:

Moment().format('LL'); // => April 29, 2016

回答by Sahil Jain

The correct way would be to specify the input as per your requirement which will give you more flexibility.

正确的方法是根据您的要求指定输入,这将为您提供更大的灵活性。

The present definition includes the following

目前的定义包括以下内容

LTS : 'h:mm:ss A', LT : 'h:mm A', L : 'MM/DD/YYYY', LL : 'MMMM D, YYYY', LLL : 'MMMM D, YYYY h:mm A', LLLL : 'dddd, MMMM D, YYYY h:mm A'

LTS : 'h:mm:ss A', LT : 'h:mm A', L : 'MM/DD/YYYY', LL : 'MMMM D, YYYY', LLL : 'MMMM D, YYYY h:mm A', LLLL : 'dddd, MMMM D, YYYY h:mm A'

You can use any of these or change the input passed into moment().format(). For example, for your case you can pass moment.utc(dateTime).format('MMMM D, YYYY').

您可以使用其中任何一个或更改传递到 moment().format() 的输入。例如,对于您的情况,您可以通过moment.utc(dateTime).format('MMMM D, YYYY').

回答by AmGates

formatCalendarDate = function (dateTime) {
    return moment.utc(dateTime).format('LL')
}

回答by Torben Rahbek Koch

With newer versions of moment.js you can also do this:

使用较新版本的 moment.js,您还可以执行以下操作:

var dateTime = moment();

var dateValue = moment({
    year: dateTime.year(),
    month: dateTime.month(),
    day: dateTime.date()
});

See: http://momentjs.com/docs/#/parsing/object/.

请参阅:http: //momentjs.com/docs/#/parsing/object/

回答by Hashmita Raut

You can also use this format:

您也可以使用这种格式:

moment().format('ddd, ll'); // Wed, Jan 4, 2017

moment().format('ddd, ll'); // Wed, Jan 4, 2017

回答by Mix Master Mike

Okay, so I know I'm way late to the party. Like 6 years late but this was something I needed to figure out and have it formatted YYYY-MM-DD.

好的,所以我知道我参加派对已经很晚了。就像晚了 6 年,但这是我需要弄清楚的事情,并将其格式化为 YYYY-MM-DD。

moment().format(moment.HTML5_FMT.DATE); // 2019-11-08

You can also pass in a parameter like, 2019-11-08T17:44:56.144.

您还可以传入一个参数,例如,2019-11-08T17:44:56.144

moment("2019-11-08T17:44:56.144").format(moment.HTML5_FMT.DATE); // 2019-11-08

https://momentjs.com/docs/#/parsing/special-formats/

https://momentjs.com/docs/#/parsing/special-formats/

回答by AJ Richardson

For people like me want the long date format (LLLL) but without the time of day, there's a GitHub issue for that: https://github.com/moment/moment/issues/2505. For now, there's a workaround:

对于像我这样想要长日期格式 ( LLLL) 但没有时间的人来说,有一个 GitHub 问题:https: //github.com/moment/moment/issues/2505。目前,有一个解决方法:

var localeData = moment.localeData( moment.locale() ),
    llll = localeData.longDateFormat( 'llll' ),
    lll = localeData.longDateFormat( 'lll' ),
    ll = localeData.longDateFormat( 'll' ),
    longDateFormat = llll.replace( lll.replace( ll, '' ), '' );
var formattedDate = myMoment.format(longDateFormat);

回答by acucchieri

You can use this constructor

你可以使用这个构造函数

moment({h:0, m:0, s:0, ms:0})

http://momentjs.com/docs/#/parsing/object/

http://momentjs.com/docs/#/parsing/object/

console.log( moment().format('YYYY-MM-DD HH:mm:ss') )

console.log( moment({h:0, m:0, s:0, ms:0}).format('YYYY-MM-DD HH:mm:ss') )
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>

回答by Adrian Grzywaczewski

Whenever I use the moment.jslibrary I specify the desired format this way:

每当我使用moment.js库时,我都会以这种方式指定所需的格式:

moment(<your Date goes here>).format("DD-MMM-YYYY")

or

或者

moment(<your Date goes here>).format("DD/MMM/YYYY")

... etc I hope you get the idea

...等我希望你明白

Inside the format function, you put the desired format. The example above will get rid of all unwanted elements from the date such as minutes and seconds

在 format 函数中,您放置所需的格式。上面的例子将从日期中删除所有不需要的元素,例如分钟和秒