如何使用 moment.js 将当前日期更改为这种格式(DD/MM/YYYY)?
我已经尝试过下面的代码。
$scope.SearchDate = moment(new Date(), "DD/MM/YYYY");
但它回来了
0037-11-24T18:30:00.000Z
。没有帮助格式化当前日期。
你可以用这个
moment().format("DD/MM/YYYY");
但是,这会返回今天指定格式的日期字符串,而不是时刻日期对象。执行以下操作将使其成为您想要的格式的时刻日期对象。
var someDateString = moment().format("DD/MM/YYYY");
var someDate = moment(someDateString, "DD/MM/YYYY");
这对我有用
var dateToFormat = "2018-05-16 12:57:13"; //TIMESTAMP
moment(dateToFormat).format("DD/MM/YYYY"); // you get "16/05/2018"
这实际上对我有用:
moment(mydate).format('L');
对于正在使用
react-moment
的任何人:
只需使用
format
支持您所需的格式:
const now = new Date()
<Moment format="DD/MM/YYYY">{now}</Moment>
安全的方法
moment.locale('en-US');
moment().format("L");
“2021年6月23日”
moment.locale('fr');
moment().format("L");
“2021年6月23日”
最好的方法是:
let today = new Date();
let todayFormatted = moment(today).format('DD-MM-YYYY');
let startDateFormatted = moment(startDate).format('DD-MM-YYYY');
const isStartDateToday = (todayFormatted === startDateFormatted);