因此后端将日期作为FE的日期:'2019-05-30T00:00:00.000Z'
我正在尝试在FE上进行渲染, 2019年5月30日
所以...
let date = new Date( Date.parse('2012-01-26T13:51:50.417-07:00') );
我一直在尝试使用MDN Javascript Date documentation
但是您甚至如何开始修改Date对象并控制到所需的输出?
答案 0 :(得分:1)
感谢您对此问题的所有贡献。
我设法为此创建了一个工作功能:
formatDate(value) {
let date = new Date(value);
const day = date.toLocaleString('default', { day: '2-digit' });
const month = date.toLocaleString('default', { month: 'short' });
const year = date.toLocaleString('default', { year: 'numeric' });
return day + '-' + month + '-' + year;
}
答案 1 :(得分:0)
function formatDate(dateString) {
let date = new Date(dateString),
day = date.getDay(),
month = date.getMonth(),
year = date.getFullYear(),
months = ['January','February','March','April','May','June','July','August','September','October','November','December'];
return day + '-' + months[month] + '-' + year;
}
预定义函数和数组。
答案 2 :(得分:-1)
这使用toLocaleString()
方法获取月份名称,否则,您将需要一个月份名称数组来匹配date.getMonth()
的输出,该输出仅是数字(索引为0)(例如一月将是0)
const monthNames = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
];
month = monthNames[date.getMonth()];
let date = new Date( Date.parse('2012-01-26T13:51:50.417-07:00') );
console.log(`${date.getDate()}-${date.toLocaleString('default', { month: 'long' })}-${date.getFullYear()}`);
另一种方法是在其余格式中也使用toLocaleString()
:
let date = new Date( Date.parse('2012-01-26T13:51:50.417-07:00') );
dateFormatted = date.toLocaleString('default',{day: 'numeric', month: 'long', year: 'numeric'});
console.log(dateFormatted);
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleString