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

后端 未结 1 810
余生分开走
余生分开走 2021-01-27 09:40

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

相关标签:
1条回答
  • 2021-01-27 10:11

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

    0 讨论(0)
提交回复
热议问题