Javascript可以';t将日期格式化为工作日,后跟日期
当使用javascript内置的Intl.DateTimeFormat格式化日期时,如果提供格式Javascript可以';t将日期格式化为工作日,后跟日期,javascript,format,datetime-format,Javascript,Format,Datetime Format,当使用javascript内置的Intl.DateTimeFormat格式化日期时,如果提供格式{weekday:'short',day'numeric'},它将重新安排这两种格式,并始终给出day,然后是weekday 供参考: console.log(新的Intl.DateTimeFormat('en-US'){ 工作日:“短”, 日期:“数字” }).格式(新日期)en-US始终是天/工作日,=>使用en-GB let date3may=新日期('2019年5月3日15:24:00')
{weekday:'short',day'numeric'}
,它将重新安排这两种格式,并始终给出day,然后是weekday
供参考:
console.log(新的Intl.DateTimeFormat('en-US'){
工作日:“短”,
日期:“数字”
}).格式(新日期)代码>en-US
始终是天/工作日,=>使用en-GB
let
date3may=新日期('2019年5月3日15:24:00'),
选项={工作日:'short',工作日:'numeric'};
log(新的Intl.DateTimeFormat('en-GB',Options.)。格式(date3may))代码>输出顺序由语言环境定义
如果要避免连接,可以使用不同的英语区域设置,例如en AU
:
var-date=新日期();
console.log(新的Intl.DateTimeFormat('en-AU'){
工作日:“短”,
日期:“数字”
}).格式(日期)代码>根据,实现您想要的结果一致的正确方法是使用Intl.DateTimeFormat.prototype.formatParts()
方法,然后手动操作给定的数组
我的第一个办法如下:
let date = new Date();
let order = ['weekday', 'day'];
new Intl.DateTimeFormat('en-US', {
weekday: 'short',
day: 'numeric'
}).formatToParts(date).filter((elem) => {
return (elem.type !== 'literal');
}).sort((a, b) => {
// SET ORDER
let i = 0;
a.index = -1;
b.index = -1;
order.forEach((type) => {
if (a.type === type) {
a.index = i;
}
if (b.type === type) {
b.index = i;
}
i++;
});
return (a.index - b.index);
}).map(({type, value}) => {
// MODIFY ELEMENT
switch (type) {
default : return (value);
}
}).reduce((string, part) => string + ' ' + part);
Intl.DateTimeFormat
没有为您提供指定日期各部分顺序的方法,它使用区域设置的预期顺序。不过,对于工作日和工作日没有标准订单。打印日期不是一种常见的方式。对象中属性的顺序被认为是不重要的。重要的是这些属性是否存在。没有反馈!!