英文:
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'm trying to convert data using JS ES6 ```Intl.DateTimeFormat("pt-BR")```, but everything i get is the previous day. Code i'm using:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-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"
<!-- end snippet -->
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
>要使用的时区。实现必须识别的唯一值是“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
>The time zone to use. The only value implementations must recognize is "UTC"; the default is the runtime's default time zone. Implementations may also recognize the time zone names of the IANA time zone database, such as "Asia/Shanghai", "Asia/Kolkata", "America/New_York".
</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):
> 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:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
var a = new Intl.DateTimeFormat("en-US");
var b = new Intl.DateTimeFormat("pt-BR");
let date = new Date(..."2015-01-02".match(/\d+/g).map((d, i) => d-i%2));
console.log(a.format(date)); // "1/1/2015"
console.log(b.format(date)); // "01/01/2015"
<!-- end snippet -->
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论