如何在toLocaleDateString中使日期字段可选

时间:2015-04-07 13:36:17

标签: javascript flot

我想在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'});

但这不起作用。有什么想法吗?

1 个答案:

答案 0 :(得分:4)

您不能只是向对象添加变量。你需要设置密钥。

var config = {month:'short', year:'2-digit'};

if (item.series.xaxis.tickSize[1] == 'day') {
      config.day = "2-digit";
}
dateString = d.toLocaleDateString(navigator.language, config);