Javascript Moment.js 中的 DD/MM/YYYY 日期格式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29861699/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 04:08:48  来源:igfitidea点击:

DD/MM/YYYY Date format in Moment.js

javascriptangularjsmomentjs

提问by Ramesh Rajendran

How can i change the current date to this format(DD/MM/YYYY) using moment.js?

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

I have tried below code.

我试过下面的代码。

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

But it's return 0037-11-24T18:30:00.000Z. Did't help to format current date.

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

回答by Arun P Johny

You need to call format()function to get the formatted value

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

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

The syntax you have used is used to parsea given string to date object by using the specified formate

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

回答by Shateel Ahmed

You can use this

你可以用这个

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

However, this returns a date string in the specified format for today, not a moment date object. Doing the following will make it a moment date object in the format you want.

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

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

回答by t_plusplus

This actually worked for me:

这实际上对我有用:

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

回答by atfede

This worked for me

这对我有用

var dateToFormat = "2018-05-16 12:57:13"; //TIMESTAMP

moment(dateToFormat).format("DD/MM/YYYY"); // you get "16/05/2018"