JavaScript ES6 Intl. 日期/时间格式化

huangapple go评论76阅读模式
英文:

Javascript ES6 Intl. Date/Time Formatting

问题

我尝试使用JS ES6的`Intl.DateTimeFormat("pt-BR")`来转换数据但我得到的都是前一天的日期我正在使用的代码如下

```js
var a = new Intl.DateTimeFormat("en-US");
var b = new Intl.DateTimeFormat("pt-BR");
console.log(a.format(new Date("2015-01-02"))); // "1/1/2015"
console.log(b.format(new Date("2015-01-02"))); // "01/01/2015";

提前感谢您。


<details>
<summary>英文:</summary>

I&#39;m trying to convert data using JS ES6 ```Intl.DateTimeFormat(&quot;pt-BR&quot;)```, but everything i get is the previous day. Code i&#39;m using:

&lt;!-- begin snippet: js hide: false console: true babel: false --&gt;

&lt;!-- language: lang-js --&gt;

    var a = new Intl.DateTimeFormat(&quot;en-US&quot;);
    var b = new Intl.DateTimeFormat(&quot;pt-BR&quot;);
    console.log(a.format(new Date(&quot;2015-01-02&quot;))); // &quot;1/1/2015&quot;
    console.log(b.format(new Date(&quot;2015-01-02&quot;))); // &quot;01/01/2015&quot;


&lt;!-- end snippet --&gt;

Thank you in advance.

</details>


# 答案1
**得分**: 0

问题与时区有关。输出是根据您的时区计算的,在UTC的1月2日00:00对应于您的时区是1月1日21:00,考虑到您的时区,例如,America/Sao_Paulo。

参考:[Mozilla文档](https://developer.mozilla.org/pt-BR/docs/Web/JavaScript/Reference/Global_Objects/DateTimeFormat)

timeZone
&gt;要使用的时区。实现必须识别的唯一值是“UTC”;默认值是运行时的默认时区。实现还可以识别IANA时区数据库的时区名称,如“Asia/Shanghai”,“Asia/Kolkata”,“America/New_York”。

<details>
<summary>英文:</summary>

The problem is related to timezone. The output is in your timezone, at 00:00 of the day 2nd of January UTC is 1st January 21:00 at your timezone, considering that your timezone is, for example, America/Sao_Paulo.

see: [doc from mozilla](https://developer.mozilla.org/pt-BR/docs/Web/JavaScript/Reference/Global_Objects/DateTimeFormat)

timeZone
&gt;The time zone to use. The only value implementations must recognize is &quot;UTC&quot;; the default is the runtime&#39;s default time zone. Implementations may also recognize the time zone names of the IANA time zone database, such as &quot;Asia/Shanghai&quot;, &quot;Asia/Kolkata&quot;, &quot;America/New_York&quot;.

</details>



# 答案2
**得分**: 0

根据[mdn](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date#Timestamp_string)的建议,不鼓励依赖`Date`解析字符串。使用`Date`构造函数(以及与其工作方式相同的`Date.parse()`)解析日期字符串由于浏览器差异和不一致性而强烈不建议。

您使用的格式(yyyy-mm-dd)被解释为UTC日期(午夜),因此与您的本地日期不对应。

所以最好自己将字符串拆分成数值参数,并将这些参数传递给`Date`构造函数:

<details>
<summary>英文:</summary>

It is discouraged to rely on `Date` parsing a string. According to [mdn](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date#Timestamp_string):

&gt; Parsing of date strings with the `Date` constructor (and `Date.parse()`, which works the same way) is strongly discouraged due to browser differences and inconsistencies. 

The format you use (yyyy-mm-dd) is interpreted as a UTC date (at midnight), so it does not correspond with your local date. 

So better break down the string yourself into numerical arguments, and pass those to the `Date` constructor:

&lt;!-- begin snippet: js hide: false console: true babel: false --&gt;

&lt;!-- language: lang-js --&gt;

    var a = new Intl.DateTimeFormat(&quot;en-US&quot;);
    var b = new Intl.DateTimeFormat(&quot;pt-BR&quot;);
    let date = new Date(...&quot;2015-01-02&quot;.match(/\d+/g).map((d, i) =&gt; d-i%2));
    console.log(a.format(date)); // &quot;1/1/2015&quot;
    console.log(b.format(date)); // &quot;01/01/2015&quot;


&lt;!-- end snippet --&gt;



</details>



huangapple
  • 本文由 发表于 2020年1月4日 02:00:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/59583222.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定