如何在 toLocaleDateString 中使日期字段可选
How to make day field optional in toLocalDateString
我想在 toLocalDateString 中将 day 参数设为可选,如下所示:
if (item.series.xaxis.tickSize[1] == 'day') {
var day_exist = 'day:"2-digit",';
}
else {
day_exist = '';
}
dateString = d.toLocaleDateString(
navigator.language, {day_exist month:'short', year:'2-digit'});
但这不起作用。有什么想法吗?
您不能只向对象添加变量。您需要设置密钥。
var config = {month:'short', year:'2-digit'};
if (item.series.xaxis.tickSize[1] == 'day') {
config.day = "2-digit";
}
dateString = d.toLocaleDateString(navigator.language, config);
我想在 toLocalDateString 中将 day 参数设为可选,如下所示:
if (item.series.xaxis.tickSize[1] == 'day') {
var day_exist = 'day:"2-digit",';
}
else {
day_exist = '';
}
dateString = d.toLocaleDateString(
navigator.language, {day_exist month:'short', year:'2-digit'});
但这不起作用。有什么想法吗?
您不能只向对象添加变量。您需要设置密钥。
var config = {month:'short', year:'2-digit'};
if (item.series.xaxis.tickSize[1] == 'day') {
config.day = "2-digit";
}
dateString = d.toLocaleDateString(navigator.language, config);