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

后端 未结 11 2176
予麋鹿
予麋鹿 2020-11-28 03:04
formatCalendarDate = function (dateTime) {
    return moment.utc(dateTime).format(\'LLL\');
};

It displays: \"28 februari 2013 09:24\"

But

相关标签:
11条回答
  • 2020-11-28 03:36

    Try this:

    moment.format().split("T")[0]
    
    0 讨论(0)
  • 2020-11-28 03:38

    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);
    
    0 讨论(0)
  • 2020-11-28 03:39

    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

    0 讨论(0)
  • 2020-11-28 03:46

    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 : 'ffffdd, 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').

    0 讨论(0)
  • 2020-11-28 03:47

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

    0 讨论(0)
  • 2020-11-28 03:49

    You can also use this format:

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

    0 讨论(0)
提交回复
热议问题