在JavaScript中将toLocaleString与数字,数组或日期一起使用

news/2024/7/7 11:29:06

toLocaleString is a built-in JavaScript method used to convert the date and time to a string using the system locales. 🤓🚀

toLocaleString是内置JavaScript方法,用于使用系统区域设置将日期和时间转换为字符串。 🤓🚀

It can be used with the following JavaScript types 💪:

可以与以下JavaScript类型💪一起使用:

  • Dates/Time

    日期/时间
  • Numbers

    号码
  • Objects

    对象
  • Arrays

    数组

带有日期和时间的toLocaleString (toLocaleString with Dates and Time 🚀)

With dates/time objects, toLocaleString has a syntax like this and returns a string 🔥👉:

对于日期/时间对象, toLocaleString具有如下语法,并返回字符串🔥👉:

dateObject.toLocaleString(locales, options)
  • locales: An optional string that specifies a language-specific format. Some valid values are ar-SA (for Arabic), en-US (for US English), hi-IN (for Hindi), jp-JP (for Japanese), etc.

    locales :一个可选的字符串,指定特定于语言的格式。 一些有效值是ar-SA (对于阿拉伯语), en-US (对于美国英语), hi-IN (对于印地语), jp-JP (对于日语)等。

  • options: An optional object of options. Some valid properties that can be included in this are dateStyle with values of full, long, medium and short. Other possible properties are timeStyle, weekday, year, month, day, hour, minute, second, etc.

    optionsoptions的可选对象。 可以包含在其中的一些有效属性是dateStyle ,其值为fulllongmediumshort 。 其他可能的属性是timeStyleweekdayyearmonthdayhourminutesecond等。

例子😍 (Example 😍)

const date = new Date();

console.log(date.toLocaleString(`en-US`)); 
// 11/10/2019, 4:32:44 PM

console.log(date.toLocaleString(`hi-IN`));
// 10/11/2019, 4:32:44 pm

console.log(date.toLocaleString(`fr-CH`));
// 10.11.2019 à 16:32:44

const options = {
  weekday: 'long',
  era: 'long'
}

console.log(date.toLocaleString(`en-US`, options)); 
// Sunday Anno Domini

console.log(date.toLocaleString(`hi-IN`, options));
// ईसवी सन रविवार

console.log(date.toLocaleString(`fr-CH`, options));
// après Jésus-Christ dimanche


带数字的toLocaleString🚀 (toLocaleString with Numbers 🚀)

With numbers, toLocaleString is used to convert the numbers into a locale-specific number representation. It has syntax something like the following and returns a string 🔥👉:

对于数字, toLocaleString用于将数字转换为特定于语言环境的数字表示形式。 它具有如下语法,并返回字符串 🔥👉:

number.toLocaleString(locales, options)
  • locales: An optional string the specifies the locale.

    locales :可选字符串,指定区域设置。

  • options: An optional object that can contain properties such as localeMatcher with values lookup and best fit. Other valied properties are style, currency, useGrouping, minimumSignificantDigits, etc.

    options :一个可选对象,可以包含具有值lookupbest fit值的属性(例如localeMatcher 。 其他标称属性是stylecurrencyuseGroupingminimumSignificantDigits等。

例子😍 (Example 😍)

const number = 12345.678;

console.log(number.toLocaleString('en-US')); 
// 12,345.678

console.log(number.toLocaleString('fr-FR')); 
// 12 345,678

console.log(number.toLocaleString('en-US', {
  style: 'currency',
  currency: 'USD'   // With currency, the currency code is also required
}));  // $12,345.68

console.log(number.toLocaleString('hi-IN', {
  style: 'currency',
  currency: 'INR'
}));  // ₹12,345.68

console.log(number.toLocaleString('en-US', {
  style: 'currency',
  currency: 'USD',
  maximumSignificantDigits: 2
}));  // $12,000


带有数组的toLocaleString (toLocaleString with Arrays 🚀)

With arrays, toLocaleString is used to convert them into a locale-specific representation. The syntax is as follows and once again a string is returned 🔥👉:

对于数组, toLocaleString用于将它们转换为特定于语言环境的表示形式。 语法如下,并再次返回一个字符串 🔥👉:

array.toLocaleString(locales, options)
  • locales: An optional string specifying the locale.

    locales :一个可选的字符串,指定区域设置。

  • options: An optional object of the same options available to numbers and dates.

    options :一个可选对象,具有与数字和日期相同的选项。

例子😍 (Example 😍)

const arr = [12345678, new Date(), "alligators"];
console.log(arr.toLocaleString(`fr-FR`,{
  style: 'currency',
  currency: 'EUR',
  era: 'long'
}));

//  12 345 678,00 €,10 11 2019 après Jésus-Christ à 18:30:03,alligators

const arr2 = [12345678, new Date(), "alligators"];
console.log(arr.toLocaleString(`en-US`,{
  style: 'currency',
  currency: 'USD',
  era: 'long'
}));

//  $12,345,678.00,11 10, 2019 Anno Domini, 6:31:56 PM,alligators

Note: If locale is omitted or left undefined than the default system locale is used. 🧪

注意:如果省略了语言环境或未定义语言环境,则使用默认的系统语言环境。 🧪



🥓 Now what’s left is to make sure your targeted browsers support the toLocaleString method.

🥓现在剩下的就是确保目标浏览器支持toLocaleString方法 。

翻译自: https://www.digitalocean.com/community/tutorials/js-using-tolocalestring


http://www.niftyadmin.cn/n/3649301.html

相关文章

Android开发过程中的几个小知识点

1. 在程序的Manifest里面对应的Activity里面添加android:windowSoftInputMode"adjustResize"属性,可以实现打开输入法时,界面自动上移,不被输入法遮盖。 2. 添加按钮的按下效果时,可以在drawable文件夹下新建一个xml文件…

[dotNET]如何利用ConfigurationSettings.AppSettings.GetValues读取配置文件中多个同Key的value

编写者:郑昀Ultrapower默认情况下,string[] strArray System.Configuration.ConfigurationSettings.AppSettings.GetValues("Uri");是无法读取配置文件中多个同Key的value的。如下所示的配置:用MSDN告诉我们的GetValues是读不到的…

node.js 模块_如何创建Node.js模块

node.js 模块The author selected the Open Internet/Free Speech Fund to receive a donation as part of the Write for DOnations program. 作者选择了“ 开放互联网/言论自由基金会”作为“ Write for DOnations”计划的一部分来接受捐赠。 介绍 (Introduction) In Node.j…

[收藏]Matt Powell的《Server-Side 异步Web Methhods》

Server-Side 异步Web Methhods http://msdn.microsoft.com/library/en-us/dnservice/html/service10012002.asp?frametrueMatt PowellMicrosoft CorporationOctober 2, 2002摘要:Matt Powell 介绍了如何在服务器端使用异步 Web 方法,来创建高性能的 Mic…

yarn命令和npm命令_npm vs纱线命令备忘单

yarn命令和npm命令Here’s a cheat sheet you can use as a handy reference for npm & Yarn. 这是一份备忘单,您可以将其用作npm& Yarn的方便参考。 There’s a lot of similarities between npm and Yarn. As the newer technology Yarn (release…

新建项目与数据库搭建

#新建项目与数据库配置 ### 一、新建项目 ### File——Gradle——JAVAWEB——勾选1和2,选择use local gradle distribution ### 二。添加依赖,build.gradle ### dependencies { // Gson json compile com.google.code.gson:gson:2.8.0 …

[EntLib]在SR.Strings中使用中文字符串资源

编写者:郑昀UltraPower要想在SR.Strings中使用中文字符串资源,必须这样:把你的SR.Strings文件保存为UTF-8编码的(具体操作是:VS.Net2003->文件菜单-高级保存选项,选择“Unicode(UTF-8 带签名) - 代码页 65001”)&am…

浅谈Android客户端项目框架

写Android也有些时间了,一边工作,一边学习,一边积累,只有遇到问题了,花时间去研究,自己的能力才能提升,刀如果不用,慢慢的就会生锈应该也是这个道理吧!上个月公司项目服务…