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

问题描述 投票:0回答:7

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

我已经尝试过下面的代码。

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

但它回来了

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

javascript angularjs momentjs
7个回答
167
投票

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

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

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


49
投票

你可以用这个

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

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

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

15
投票

这对我有用

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

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

10
投票

这实际上对我有用:

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

4
投票

对于正在使用

react-moment
的任何人:

只需使用

format
支持您所需的格式:

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

0
投票

安全的方法

moment.locale('en-US');
moment().format("L"); 

“2021年6月23日”

moment.locale('fr');
moment().format("L");

“2021年6月23日”


0
投票

最好的方法是:

let today = new Date();
let todayFormatted = moment(today).format('DD-MM-YYYY');
let startDateFormatted = moment(startDate).format('DD-MM-YYYY');
const isStartDateToday = (todayFormatted === startDateFormatted);
© www.soinside.com 2019 - 2024. All rights reserved.