可以使用moment.js库中的format()方法来格式化输出日期和时间。

示例代码:

var now = moment();
var formattedDate = now.format('YYYY-MM-DD HH:mm:ss');
console.log(formattedDate);

输出结果:

2021-07-12 16:30:45

在format()方法中,使用大写的“Y”表示年份,小写的“m”表示月份,大写的“D”表示日期,大写的“H”表示小时(24小时制),小写的“m”表示分钟,小写的“s”表示秒。

你可以根据需要自定义格式,例如:

var now = moment();
var formattedDate = now.format('YYYY/MM/DD hh:mm A');
console.log(formattedDate);

输出结果:

2021/07/12 04:30 PM

在这个例子中,使用斜杠“/”作为日期分隔符,使用小写的“h”表示小时(12小时制),大写的“A”表示上午或下午。

momentjs格式化输出年月日 小时分

原文地址: https://www.cveoy.top/t/topic/bRkx 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录