如何根据地区确定年,月,日的顺序? [英] how to determine year, month, day ordering based on locale?

查看:75
本文介绍了如何根据地区确定年,月,日的顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

如何确定显示哪个顺序

仅使用JavaScript(不是瞬间)的年,月和日吗? $ c> zh-CN :

Example output for 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"]

这篇关于如何根据地区确定年,月,日的顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆