我想显示三个自定义下拉菜单,供用户选择其身份证的年,月和日。

如何仅使用JavaScript(而非moment)确定显示年,月和日的顺序?

en-US的示例输出:

['month', 'day', 'year']


en-CA的示例输出:

['year', 'month', 'day']

最佳答案

function datePartOrder(locale) {
  const parts = new Intl.DateTimeFormat(locale).formatToParts();
  const filteredParts = parts.filter(part => ['year', 'month', 'day'].includes(part.type));
  const filteredPartNames = filteredParts.map(part => part.type);
  return filteredPartNames;
}

// examples:

console.log(datePartOrder('en-US')); //["month", "day", "year"] );
console.log(datePartOrder('en-CA')); //["year", "month", "day"]

关于javascript - 如何根据地区确定年,月,日的顺序?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51433605/

10-12 12:21
查看更多