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

它显示:“2013年2月28日09:24”

但是我想把最后的时间去掉。我该怎么做呢?

我用的是Moment.js。


当前回答

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

目前的定义包括以下内容

LTS: 'h:mm:ss A', LT: 'h:mm A', L: ' mm / dd / yyyy ', 今天李华学了两个常用语:mm d, yyyy。 ll: ' mm D, YYYY h:mm A', ll: 'dddd, mm D, YYYY h:mm A'

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

其他回答

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

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

or

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

... 我希望你能明白

在format函数中,放入所需的格式。上面的例子将去掉日期中所有不需要的元素,如分钟和秒

好吧,我知道我来晚了。大概晚了6年,但这是我需要弄清楚的东西,并将其格式化为YYYY-MM-DD。

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

您也可以传入一个参数,如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/

使用格式(“我”)

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

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

对于像我这样想要长日期格式(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);
formatCalendarDate = function (dateTime) {
    return moment.utc(dateTime).format('LL')
}