Intl.DateTimeFormat月“短”与句点

时间:2015-09-23 01:04:18

标签: javascript

使用Intl.DateTimeFormat时,是否可以在缩写的“短”月份名称后添加句点?

如果可能的话,我也更喜欢更常见的en-us月缩写Sept.而不是SepSep.

var dateStr = '2015-09-22';
var dateObj = new Date(dateStr);
var options = { year: 'numeric', month: 'short', day: 'numeric' };
new Intl.DateTimeFormat('en-US', options).format(dateObj);

// outputs: Sep 22, 2015
// better: Sep. 22, 2015
// preferred: Sept. 22, 2015

2 个答案:

答案 0 :(得分:1)

这取决于系统的区域设置或区域设置,脚本或脚本作者 无法控制。好吧,你可以对此做一个i18n:

monthsShort= 'Jan_Feb_Mar_Apr_May_Jun_Jul_Aug_Sep_Oct_Nov_Dec'.split('_')
               .map(function (month) {
                 return month + ".";
               });
顺便说一句,我还没有看到 Janu。 Febu。 LoL。

答案 1 :(得分:1)

可以使用小函数重新格式化日期字符串。这避免了解析和时区的任何问题,并且意味着您可以使用任何形式的缩写和句点:

function reformatDateString(s) {
      var months = ['Jan.','Feb.','Mar.','Apr.','May.','Jun.','Jul.','Aug.','Sep.','Oct.','Nov.','Dec.'];
      var parts = s.split('-');
      return months[parts[1] - 1] + ' ' + Number(parts[1]) + ', ' + parts[0];
}

document.write(reformatDateString('2015-09-15')); // Sep. 9, 2015

相关问题