【问题标题】:How can I remove time from date with Moment.js?如何使用 Moment.js 从日期中删除时间?
【发布时间】:2023-03-22 04:57:01
【问题描述】:
formatCalendarDate = function (dateTime) {
    return moment.utc(dateTime).format('LLL');
};

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

但我想删除最后的时间。我该怎么做?

我正在使用Moment.js

【问题讨论】:

  • 使用Split方法分隔字符串

标签: javascript date momentjs


【解决方案1】:
moment(date).format(DateFormat)

这里的 DateFormat 应该是DateFormat = 'YYYY-MM-DD'

【讨论】:

  • 您好,欢迎来到 SO。考虑让Tour 了解如何回答问题。请提供有关您的解决方案的更多信息并使用 Stack Snippet 显示您的代码。
【解决方案2】:

我迟到了,但这对我来说很完美:

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

【讨论】:

    【解决方案3】:

    看看这些例子。

    格式化日期

    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
    

    相对时间

    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
    

    日历时间

    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
    

    多语言环境支持

    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
    

    【讨论】:

      【解决方案4】:

      好的,所以我知道我迟到了。好像晚了 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/

      【讨论】:

        【解决方案5】:

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

        .startOf('day')
        

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

        【讨论】:

        • 如果您在时区之间移动(或者如果您不注意时区),请注意这一点。我遇到了一个问题,我的 UTC 日期被转换为当地时间,然后应用startOf('day'),这是前一天的开始。已用moment(moment.utc('2013-10-29T00:00:00+00:00').startOf('day').format('LL')).startOf('day').toDate() 修复
        • 还要注意这个函数实际上改变了原始对象
        • .startOf('day') 本身并没有删除时间部分,它只是将时间设置为 00:00:00。所以,是的,正如 'collin' 评论的那样,保存日期时必须小心。更好的选择是使用format('LL'),正如本帖中已回答的那样。
        • 为避免改变原始对象,请使用someMoment.clone().startOf('day')moment(someMoment).startOf('day')
        • 我还必须处理多个时区,但发现只需字符串 utc().startOf('day') 就足够了: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
        【解决方案6】:

        你可以使用这个构造函数

        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') )
        <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>

        【讨论】:

        • 这里添加一些矿石描述
        • 这很有趣!它创造了一个时刻,超越了小时、分钟和秒,但仍然保持着下属的日期。很有趣,因为我猜到 moment({h:0, m:0, s:0, ms:0}) 会给我 1970 年 1 月 1 日而不是今天。
        • 看起来moment({ h: 0 }) 也一样。
        • 文档说Omitted units default to 0 or the current date, month, and year.
        【解决方案7】:

        使用format('LL')

        根据您尝试用它做什么,format('LL') 可以解决问题。它产生这样的东西:

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

        【讨论】:

        【解决方案8】:

        对于像我这样想要长日期格式 (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);
        

        【讨论】:

        • 终于给出了一个明智的答案。接受我的投票,先生!这个问题似乎还没有得到解决。
        【解决方案9】:

        使用较新版本的 moment.js,您也可以这样做:

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

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

        【讨论】:

        • 会不会是date: dateTime.date() 而不是day: dateTime.date()
        • 'day 和 date 键均表示月份中的某一天。'从文档。 day 工作于 2.8.4 之前的版本。
        【解决方案10】:

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

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

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

        ...等我希望你能明白

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

        【讨论】:

        • 如果您想在不同的语言环境中显示您的内容,这不是一个好主意。如果您想以正确的格式显示用户区域设置的日期,则需要使用预设的日期格式之一(LLL 等)
        • 为什么要设置 MMM 3 次。
        • MMM 会给你当月的前 3 个字母。 'Apr' MMMM 会给你一个完整的月份名称
        【解决方案11】:

        试试这个:

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

        【讨论】:

        • 注意这种方法,因为1993-06-07T22:00:00.000Z 将导致1993-06-07 而它是1993-06-08 一天的开始
        【解决方案12】:
        formatCalendarDate = function (dateTime) {
            return moment.utc(dateTime).format('LL')
        }
        

        【讨论】:

          【解决方案13】:

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

          目前的定义包括以下内容

          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'

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

          【讨论】:

            【解决方案14】:

            您也可以使用这种格式:

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

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2012-12-26
              • 1970-01-01
              • 2017-09-09
              • 2020-01-23
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多