如何用Moment.js删除日期日期?

时间:2022-12-01 16:03:53
formatCalendarDate = function (dateTime) {
    return moment.utc(dateTime).format('LLL');
};

It displays: "28 februari 2013 09:24"

显示:“februari 28 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。

9 个解决方案

#1


401  

Sorry to jump in so late, but if you're really wanting to remove the time portion of a moment() rather than just formatting it, then the code is:

很抱歉这么晚才开始,但是如果您真的想删除moment()中的time部分,而不只是格式化它,那么代码是:

.startOf('day')

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

裁判:http://momentjs.com/docs/ /操作/开始/

#2


24  

Use 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

#3


9  

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'

lh:嗯,嗯,嗯,嗯,嗯,嗯,嗯,嗯,嗯。

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)。格式(“嗯D,YYYY”)。

#4


5  

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

#5


3  

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

更新版本的时刻。你也可以这样做:

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/ /解析/对象/。

#6


2  

You can also use this format:

你也可以使用以下格式:

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

时刻()。格式(“ddd、ll ');// 2017年1月4日

#7


1  

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

每当我利用这一刻。我以这种方式指定所需的格式:

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函数中,输入所需的格式。上面的示例将从日期(如分钟和秒)中删除所有不需要的元素

#8


0  

Try this:

试试这个:

moment.format().split("T")[0]

#9


0  

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);

#1


401  

Sorry to jump in so late, but if you're really wanting to remove the time portion of a moment() rather than just formatting it, then the code is:

很抱歉这么晚才开始,但是如果您真的想删除moment()中的time部分,而不只是格式化它,那么代码是:

.startOf('day')

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

裁判:http://momentjs.com/docs/ /操作/开始/

#2


24  

Use 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

#3


9  

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'

lh:嗯,嗯,嗯,嗯,嗯,嗯,嗯,嗯,嗯。

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)。格式(“嗯D,YYYY”)。

#4


5  

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

#5


3  

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

更新版本的时刻。你也可以这样做:

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/ /解析/对象/。

#6


2  

You can also use this format:

你也可以使用以下格式:

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

时刻()。格式(“ddd、ll ');// 2017年1月4日

#7


1  

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

每当我利用这一刻。我以这种方式指定所需的格式:

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函数中,输入所需的格式。上面的示例将从日期(如分钟和秒)中删除所有不需要的元素

#8


0  

Try this:

试试这个:

moment.format().split("T")[0]

#9


0  

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);