js中对new Date() 中转换字符串方法toLocaleString的使用

提供特定于区域设置的日期和时间格式。

dateTimeFormatObj = new Intl.DateTimeFormat([locales][, options])

dateTimeFormatObj

必需。将 DateTimeFormat 对象分配到的变量名。

locales

可选。包含一种或多种语言或区域设置标记的区域设置字符串数组。如果包含多个区域设置字符串,请以降序优先级对它们进行排列,确保首个条目为首选区域设置。如果省略此参数,则使用 JavaScript 运行时的默认区域设置。有关更多信息,请参见备注部分。

options

可选。包含指定日期和时间格式设置选项的一个或多个特性的对象。有关详细信息,请参见“备注”部分。

locales 参数必须符合 BCP 47 语言或“en-us”和“zh-CN”等区域设置标记。标记可包括语言、区域、国家/地区和变量。有关语言标记的示例,请参见 BCP 47 的附录 A。对于 DateTimeFormat,你可能需在区域设置字符串中添加一个 -u 子标记以包含一个或两个以下 Unicode 扩展:

  • -nu 指定编号系统扩展:language-region-u-nu-numberingsystem

    其中 numberingsystem 可为以下各项之一:阿拉伯数字、阿拉伯文数字、巴厘数字、孟加拉数字、梵文数字、全角数字、古吉拉特数字、果鲁穆奇数字、汉语数字、高棉数字、坎纳达数字、老挝数字、拉丁数字、林布数字、马拉雅拉姆数字、蒙古数字、缅甸数字、欧迪亚数字、泰米尔数字、泰卢固数字、泰语数字、藏语数字。

  • –ca 指定日历:language-region-u-ca-calendar

    其中 calendar 可为以下各项之一:佛历、农历、公历、回历及和历。

options 参数可包括以下属性:

属性说明可能的值:默认值
localeMatcher指定要使用的区域设置匹配算法。"lookup"、"best fit""best fit"
formatMatcher指定要使用的格式匹配算法。"basic"、"best fit""best fit"
hour12指定是否对小时使用 12 小时格式。true(12 小时格式)、false(24 小时格式)
timeZone指定时区。至少 "UTC" 始终受支持。一个时区值,如 "UTC"。"UTC"
weekday指定周日期的格式设置。"narrow"、"short"、"long"。undefined
era指定纪元的格式设置。"narrow"、"short"、"long"undefined
year指定年份的格式设置。"2-digit"、"numeric"undefined 或 "numeric"
month指定月份的格式设置。"2-digit"、"numeric"、"narrow"、"short"、"long"undefined 或 "numeric"
day指定日的格式设置。"2-digit"、"numeric"undefined 或 "numeric"
hour指定小时的格式设置。"2-digit"、"numeric"undefined
minute指定分钟的格式设置。"2-digit"、"numeric"undefined
second指定秒的格式设置。"2-digit"、"numeric"undefined
timeZoneName指定时区的格式设置。目前不支持此属性。"short"、"long"。目前不支持此属性。

weekdayerayearmonthdayhourminutesecond 的默认值为 undefined。如果不设置这些属性,则 yearmonthday 使用 "numeric" 格式。

每个区域设置必须至少支持以下格式:

  • 周日期、年、月、日、小时、分钟、秒
  • 周日期、年、月、日
  • 年、月、日
  • 年、月
  • 月、日
  • 小时、分钟、秒
  • 小时、分钟

下表列出了 DateTimeFormat 对象的属性。

属性说明
构造函数指定创建日期/时间格式化程序对象的函数。
format返回利用日期/时间格式化程序设置对特定于区域设置的日期设置格式的函数。
原型返回对日期/时间格式化程序原型的引用。

下表列出了 DateTimeFormat 对象的方法。

方法说明
resolvedOptions返回包含日期/时间格式化程序对象的属性和值的对象。

以下示例演示使用不同的区域设置将日期对象传递给 DateTimeFormat 的结果。

var date = new Date(Date.UTC(2013, 1, 1, 14, 0, 0));
var options = { weekday: "long", year: "numeric", month: "short",
    day: "numeric" };

if (console && console.log) {
    console.log(new Intl.DateTimeFormat("en-US").format(date));
    // Returns ‎2‎/‎1‎/‎2013
    console.log(new Intl.DateTimeFormat("ja-JP").format(date));
    // Returns ‎2013‎年‎2‎月‎1‎日
    console.log(new Intl.DateTimeFormat("ar-SA", options).format(date));
    // Returns ‏الجمعة‏, ‏٢٠‏ ‏ربيع الأول‏, ‏١٤٣٤
    console.log(new Intl.DateTimeFormat("hi-IN", options).format(date));
    // Returns ‎शुक्रवार‎, ‎01‎ ‎फरवरी‎ ‎2013
}

以下示例将创建一个 DateTimeFormat 对象,它会指定当前周日期采用长格式并使用阿拉伯语(沙特阿拉伯)区域设置、回历和拉丁语数字系统。

var dtf = new Intl.DateTimeFormat(["ar-SA-u-ca-islamic-nu-latn"], {
    weekday: "long",
    year: "numeric",
    day: "numeric",
    month: "long"
}); 

If (console && console.log) {
    console.log(dtf.format(new Date()));
    // Returns ‏الجمعة‏, ‏19‏ ‏رمضان‏, ‏1434
}

在 Internet Explorer 11 标准文档模式下支持此项。此外,也在应用商店应用(Windows 8.1 和 Windows Phone 8.1)中受支持。请参阅版本信息

在以下文档模式中不受支持:Quirks、Internet Explorer 6 标准模式、Internet Explorer 7 标准模式、Internet Explorer 8 标准模式、Internet Explorer 9 标准模式和 Internet Explorer 10 标准模式。在 Windows 8 中不受支持。

MDN

https://msdn.microsoft.com/zh-cn/library/dn342822(v=vs.94).aspx

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值