【问题标题】:DD/MM/YYYY Date format in Moment.jsMoment.js 中的 DD/MM/YYYY 日期格式
【发布时间】:2015-07-03 21:31:13
【问题描述】:

如何使用 moment.js 将当前日期更改为这种格式(DD/MM/YYYY)?

我试过下面的代码。

$scope.SearchDate = moment(new Date(), "DD/MM/YYYY");

但它是返回 0037-11-24T18:30:00.000Z。没有帮助格式化当前日期。

【问题讨论】:

    标签: javascript angularjs momentjs


    【解决方案1】:

    对于使用react-moment的任何人:

    只需将format 属性用于您需要的格式:

    const now = new Date()
    <Moment format="DD/MM/YYYY">{now}</Moment>
    

    【讨论】:

      【解决方案2】:

      这对我有用

      var dateToFormat = "2018-05-16 12:57:13"; //TIMESTAMP
      
      moment(dateToFormat).format("DD/MM/YYYY"); // you get "16/05/2018"
      

      【讨论】:

        【解决方案3】:

        你可以用这个

        moment().format("DD/MM/YYYY");
        

        但是,这将返回今天指定格式的日期字符串,而不是时刻日期对象。执行以下操作将使其成为所需格式的时刻日期对象。

        var someDateString = moment().format("DD/MM/YYYY");
        var someDate = moment(someDateString, "DD/MM/YYYY");
        

        【讨论】:

        • 我的日期格式为“MM/DD/YY”,并希望将其转换为“DD/MM/YYYY”。所以我的疑问是时刻如何知道它的格式。假设日期是'06/05/20'。时刻如何知道是 MM/DD/YY 还是 DD/MM/YY
        【解决方案4】:

        这实际上对我有用:

        moment(mydate).format('L');
        

        【讨论】:

        • 这将返回 MM/DD/YYYY。但在实际问题中,他期待 DD/MM/YYYY 格式。
        【解决方案5】:

        需要调用format()函数获取格式化后的值

        $scope.SearchDate = moment(new Date()).format("DD/MM/YYYY")
        //or $scope.SearchDate = moment().format("DD/MM/YYYY")
        

        您使用的语法用于通过使用指定的格式将给定的字符串parse 日期对象

        【讨论】:

        • 好的。 new Date() 是 javascript 代码。你能用 moment.js 给我获取当前日期吗?
        • @RameshRajendran 它为您提供当前日期 - jsfiddle.net/arunpjohny/hrrnenf2/1
        • @RameshRajendran $scope.SearchDate = moment(moment.now()).format("DD/MM/YYYY")
        • moment()moment(new Date()) 相同
        猜你喜欢
        • 1970-01-01
        • 2016-01-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-02
        • 1970-01-01
        相关资源
        最近更新 更多