【发布时间】:2018-11-01 14:11:01
【问题描述】:
我正在使用 2.22.1 来格式化日期。
当我输入来自日期选择器的日期时,moment 会将日期放回一天。例如,当我尝试按以下方式格式化日期时:
示例 1:
const day = new Date(value).getDate();
const month = new Date(value).getMonth();
const fullYear = new Date(value).getFullYear();
console.log('day', day); // 7
console.log('month', month); // 5
console.log('fullYear', fullYear); //2018
格式化函数:
moment.utc(new Date(month + ' ' + day + ' ' + fullYear), 'MM-DD-YYYY').format('DD-MM-YY')
输出:06-05-18
预计:07-05-18
示例 2:
输入:2018 年 5 月 31 日星期四 00:00:00 GMT+0100(GMT 夏令时间)
格式化函数:
moment.utc(new Date(value)).format('DD-MM-YY')
输出:30-05-18 预计:31-05-18
【问题讨论】: