how to determine year, month, day ordering based on locale?

心已入冬 提交于 2019-12-31 05:15:42

问题


I would like to display three custom dropdowns for the user to select year, month, and day of their identification card.

How can I determine which order to show the year, month, and day, using only JavaScript (not moment)?

Example output for en-US:

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

Example output for en-CA:

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

回答1:


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"]


来源:https://stackoverflow.com/questions/51433605/how-to-determine-year-month-day-ordering-based-on-locale

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!