首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >JavaScript Intl.DateTimeFormat.format vs Date.toLocaleString

JavaScript Intl.DateTimeFormat.format vs Date.toLocaleString
EN

Stack Overflow用户
提问于 2018-03-15 06:34:06
回答 1查看 14.6K关注 0票数 25

我想打印一个表示日期的字符串,使用特定的时区、区域设置和显示选项。

我应该使用其中哪一个?

  1. Intl.DateTimeFormat.prototype.format
  2. Date.prototype.toLocaleString()

看起来它们返回的结果是一样的。

const event = new Date(1521065710000);

const options = {
  day: 'numeric',
  month: 'long',
  weekday: 'short',
  hour: 'numeric',
  minute: 'numeric',
  timeZoneName: 'short',
  timeZone: 'America/Los_Angeles',
};

console.log(event.toLocaleString('en-US', options));
// "Wed, March 14, 3:15 PM PDT"

console.log(new Intl.DateTimeFormat('en-US', options).format(event));
// "Wed, March 14, 3:15 PM PDT"

EN

回答 1

Stack Overflow用户

发布于 2018-09-18 05:08:36

目前为止,所有浏览器都支持国际化API isn't supported --特别是IE10及以下版本,以及适用于安卓11.8的UC浏览器。如果您想支持这些浏览器,请使用ToLocaleString。(但是,如果不支持国际化API,ToLocalString返回的是依赖于实现的内容。)

Intl.DateTimeFormat.prototype.format是为设置大组日期的格式而设计的-而不是每次都设置区域设置和选项,您只需设置它们一次,然后从那时起使用生成的格式函数。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49288700

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档