怎样在设计网站做图赚钱,织梦网站打开速度慢,机构网站源码,公司网络维护服务JavaScript 的 Intl 对象可以方便地进行各种国际化处理#xff0c;包括货币、日期、时间和数字格式化 1、Intl.Collator
比较字符串
Options 选项值描述localeMatcherlookup , best fit确定使用哪种算法来选择语言环境usagesort #xff0c;search指定比较的用途sensitivit… JavaScript 的 Intl 对象可以方便地进行各种国际化处理包括货币、日期、时间和数字格式化 1、Intl.Collator
比较字符串
Options 选项值描述localeMatcherlookup , best fit确定使用哪种算法来选择语言环境usagesort search指定比较的用途sensitivitybase, accent, case, variant确定比较的灵敏度ignorePunctuationBoolean是否忽略标点符号numericBoolean是否进行数字排序caseFirstupper, lower, false指定大写或小写字母优先
示例
const collator new Intl.Collator(en, { sensitivity: base });
console.log(collator.compare(a, A)); // 输出: 02、Intl.DateTimeFormat
用于格式化日期和时间
Options 选项值描述localeMatcherlookup, best fit确定使用哪种算法来选择语言环境weekdaynarrow, short, long指定显示星期的格式eranarrow, short, long指定显示纪元的格式yearnumeric, 2-digit指定显示年份的格式monthnumeric, 2-digit, narrow, short, long指定显示月份的格式daynumeric, 2-digit指定显示日期的格式hournumeric, 2-digit指定显示小时的格式minutenumeric, 2-digit指定显示分钟的格式secondnumeric, 2-digit指定显示秒的格式timeZoneNameshort, long指定显示时区的格式formatMatcherbasic, best fit用于选择最佳的格式匹配算法hour12Boolean指定是否使用 12 小时制timeZone–指定时区的 IANA 时区名称
示例
const date new Date();
const formatter new Intl.DateTimeFormat(en-US, {year: numeric,month: long,day: 2-digit
});
console.log(formatter.format(date)); // 输出: June 07, 20243、Intl.NumberFormat
用于格式化数字包括货币和百分比
options 选项值描述localeMatcherlookup, best fit确定使用哪种算法来选择语言环境styledecimal, currency, percent, unit指定格式化的样式currencyUSD, EUR …指定货币代码currencyDisplaysymbol, narrowSymbol, code, name指定货币显示的格式useGroupingBoolean指定是否使用分组分隔符minimumIntegerDigitsNumber指定最少整数位数minimumFractionDigitsNumber指定最少小数位数maximumFractionDigitsNumber指定最多小数位数minimumSignificantDigitsNumber指定最少有效位数maximumSignificantDigitsNumber指定最多有效位数
示例
const number 123456.789;
const formatter new Intl.NumberFormat(en-US, {style: currency,currency: USD
});
console.log(formatter.format(number)); // 输出: $123,456.794、Intl.PluralRules
用于处理复数规则
options 选项值描述localeMatcherlookup, best fit确定使用哪种算法来选择语言环境typecardinal, ordinal指定复数规则的类型
示例
const pluralRules new Intl.PluralRules(en-US);
console.log(pluralRules.select(0)); // 输出: other
console.log(pluralRules.select(1)); // 输出: one
console.log(pluralRules.select(2)); // 输出: other5、Intl.RelativeTimeFormat
用于格式化相对时间
options 选项值描述localeMatcherlookup, best fit确定使用哪种算法来选择语言环境numericalways, auto指定是否总是使用数字stylelong, short, narrow指定相对时间的格式样式
示例
const rtf new Intl.RelativeTimeFormat(en, { numeric: auto });
console.log(rtf.format(-1, day)); // 输出: yesterday
console.log(rtf.format(1, day)); // 输出: tomorrow6、Intl.Locale
用于处理和解析 BCP 47 语言标记
options 选项值描述baseNameen-US, …基础语言环境名称calendargregory, buddhist指定日历系统caseFirstupper, lower, false指定在排序中大写或小写字母优先collationemoji, phonebook指定排序规则hourCycleh11, h12, h23, h24指定小时循环系统numericBoolean指定是否进行数字排序numberingSystemlatn, arab …指定编号系统regionUS, GB …指定区域scriptLatn, Cyrl指定书写系统
示例
const locale new Intl.Locale(en-US, { numeric: true, caseFirst: upper });
console.log(locale.baseName); // 输出: en-US