ChatGPT解决这个技术问题 Extra ChatGPT

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

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

It displays: "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.

Use Split method to separate the strings

L
Liam

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

.startOf('day')

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


Be careful with this if you're going between timezones (or if you're not paying attention to timezones). I had an issue where my UTC date was getting converted to local time, then applying startOf('day'), which was then the start of the previous day. Fixed with moment(moment.utc('2013-10-29T00:00:00+00:00').startOf('day').format('LL')).startOf('day').toDate()
Also be careful that this function actually mutates the original object
.startOf('day') does not removes the time part per se, it just sets the time to 00:00:00. So, yes, as commented by 'collin', you have to be careful when saving date. Better alternative is using format('LL'), as have been answered in this thread.
To avoid mutating the original object, use someMoment.clone().startOf('day') or moment(someMoment).startOf('day').
I also had to deal with multiple timezones but found that simply stringing utc().startOf('day') was enough: moment("2017-10-09T00:00:00Z").isSameOrAfter(moment("2017-10-09 22:00:00+00:00").utc().startOf('day'), 'day'); true moment("2017-10-09T00:00:00Z").isSameOrAfter(moment("2017-10-09 22:00:00+00:00").startOf('day'), 'day'); false
J
Joshua Pinter

Use format('LL')

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

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

See momentjs.com/docs/#/parsing/string-format for more formatting options.
S
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'

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').


M
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.

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

You can also pass in a parameter like, 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/


H
Hashmita Raut

You can also use this format:

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


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

A
Adrian Grzywaczewski

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


This is not a good idea if you ever want to display your content in different locales. If you want to display dates in the correct format for the user's locale, you need to use one of the preset date formats (L, LL, etc.)
Why you set MMM three times.
MMM will give you the 3 first letters of the month. 'Apr' MMMM will give you a full month name
Actually this is the best one if used with DD/MM/YYYY (two Ms) as this doesn't break when comparing dates with different locales, the other variants fail.
F
FlavorScape

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/.


Wouldn't it be date: dateTime.date() instead of day: dateTime.date()?
'day and date key both mean day-of-the-month.' from the docs. day works pre version 2.8.4.
A
Ankit Kumar Rajpoot

Look at these Examples.

Format Dates

moment().format('MMMM Do YYYY, h:mm:ss a'); // December 7th 2020, 9:58:18 am
moment().format('dddd');                    // Monday
moment().format("MMM Do YY");               // Dec 7th 20
moment().format('YYYY [escaped] YYYY');     // 2020 escaped 2020
moment().format();                          // 2020-12-07T09:58:18+05:30

Relative Time

moment("20111031", "YYYYMMDD").fromNow(); // 9 years ago
moment("20120620", "YYYYMMDD").fromNow(); // 8 years ago
moment().startOf('day').fromNow();        // 10 hours ago
moment().endOf('day').fromNow();          // in 14 hours
moment().startOf('hour').fromNow();       // an hour ago

Calendar Time

moment().subtract(10, 'days').calendar(); // 11/27/2020
moment().subtract(6, 'days').calendar();  // Last Tuesday at 9:58 AM
moment().subtract(3, 'days').calendar();  // Last Friday at 9:58 AM
moment().subtract(1, 'days').calendar();  // Yesterday at 9:58 AM
moment().calendar();                      // Today at 9:58 AM
moment().add(1, 'days').calendar();       // Tomorrow at 9:58 AM
moment().add(3, 'days').calendar();       // Thursday at 9:58 AM
moment().add(10, 'days').calendar();      // 12/17/2020

Multiple Locale Support

moment.locale();         // en
moment().format('LT');   // 9:58 AM
moment().format('LTS');  // 9:58:18 AM
moment().format('L');    // 12/07/2020
moment().format('l');    // 12/7/2020
moment().format('LL');   // December 7, 2020
moment().format('ll');   // Dec 7, 2020
moment().format('LLL');  // December 7, 2020 9:58 AM
moment().format('lll');  // Dec 7, 2020 9:58 AM
moment().format('LLLL'); // Monday, December 7, 2020 9:58 AM
moment().format('llll'); // Mon, Dec 7, 2020 9:58 AM

A
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:

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

Finally a sensible answer. Take my upvote sir! The issue hasn't been addressed yet it seems.
a
acucchieri

You can use this constructor

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

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


Add some ore description here
This is interesting! It's creating a moment and overriding hour, minutes and seconds but still keeping the underling date. Interesting because I would have guessed that moment({h:0, m:0, s:0, ms:0}) would have given me January 1 1970 rather than today.
Also looks like moment({ h: 0 }) does the same.
The docs say Omitted units default to 0 or the current date, month, and year.
M
Mohd

Try this:

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

Watch out with this method, as 1993-06-07T22:00:00.000Z will result as 1993-06-07 whereas it is the start of the day of 1993-06-08
A
Alex Parashchevin

The thing is - you can run into an issue with timezones. For example, if you parse date like this: '2022-02-26T00:36:21+01:00' it may turn into '25/02/2022' As a solution if your date is in ISO format you can just cut off the time portion from the string, like this:

moment('2022-02-26T00:36:21+01:00'.split('T')[0]).utc().format('DD/MM/YYYY')

This solution is quite blunt, so be careful with string format.


s
suthar pavan

Try new Date().toDateString()

Result - "Fri Jun 17 2022"


Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
A
Aviv

I am late, but this worked perfect for me:

moment().format('YYYY-MM-DD')


A
Abhinav Kumar
moment(date).format(DateFormat)

Here DateFormat should be DateFormat = 'YYYY-MM-DD'


Hello, welcome to SO. Consider making a Tour understand how to answer questions. Please, give more information about your solution and use Stack Snippet to show your code.