date-fns在Safari中返回无效日期 [英] date-fns returns invalid date on safari
问题描述
我正在使用 date-fns
返回一些值,如下所示:
import {format,formatDistance} from date-fns;
var date = new Date( 2019-03-06 00:00:00);
console.log(format(new Date(date), dd MMM,y));
在Chrome上运行良好,并返回我们3月,y
但在Safari中返回无效日期
。
我相信这是因为日期( 2019-03-06 00:00:00)的格式不是ISO 8601。但这是我从端点接收的格式。
我有两个问题:
p>
-
您第一次解析日期时就依赖于非标准输入格式。
-
您要将
Date
传递到Date
构造函数中,这迫使其进行转换日期为字符串,然后解析该字符串。
我只解析一次,并使用<调用标准日期/时间格式 >首次使用新日期
:
import {format,formatDistance} from date-fns ;
var date = new Date( 2019-03-06T00:00:00);
//注意----------------------- ^
console.log(format(date, dd MMM,y) );
//没有`new Date` ^
请注意,您的字符串将被解析为 local time (在符合规范的JavaScript引擎上),因为它包括字符串的时间部分。不幸的是,在ES2015中添加了格式,并在ES2016中进行了更新之后,格式却有所不同,但是最终的位置是:
当UTC偏移量表示为如果没有日期,则仅将日期形式解释为UTC时间,将将日期-时间形式解释为本地时间。
没有UTC偏移量(没有 Z
或 +00:00
或类似名称),并且确实有时间,它是在当地时间解析的。 (同样,使用符合规范的JavaScript引擎¹。)
我的建议是不要使用内置的 Date解析日期字符串
对象,或者确保在字符串上始终带有时区指示符。
¹ RobG 指出Safari解析 new Date( 2019-03-06T00:00:00 )
作为UTC。遗憾的是,这是Apple JavaScript引擎JavaScriptCore中的一个错误。它不仅会影响Safari,还会影响iOS上的Chrome(可能还会影响其他iOS浏览器;我已经测试了Brave,Opera和Dolphin),因为Chrome必须使用JavaScriptCore而不是iOS上的常规V8,因为应用无法分配可执行内存,因此JIT引擎不能在iOS上使用。但是V8团队已经制作了仅限解释器的V8版本,因此可能是Chrome(和Brave)在如果速度足够快,iOS将会更新为使用它。
I'm using date-fns
to return some values as shown below:
import { format, formatDistance } from "date-fns";
var date = new Date("2019-03-06 00:00:00");
console.log(format(new Date(date), "dd MMM, y"));
It works fine on Chrome and returns We Mar, y
But returns Invalid Date
in Safari.
I believe it's because the date ("2019-03-06 00:00:00") is not in ISO 8601 format. But this is the format I'm receiving from an endpoint. Is there any option to convert this to the right format and make it work on Safari?
I see two issues:
You're relying on a nonstandard input format the first time you parse the date.
You're passing a
Date
into theDate
constructor, which forces it to convert the date to a string, then parse the string.
I'd only parse it once, and use the standard date/time format when calling new Date
the first time:
import { format, formatDistance } from "date-fns";
var date = new Date("2019-03-06T00:00:00");
// Note -----------------------^
console.log(format(date, "dd MMM, y"));
// No `new Date` ^
Note that your string will be parsed as local time (on spec-compliant JavaScript engines¹) because it includes the time portion of the string. Unfortunately this varied after the format was added in ES2015, updated in ES2016, but where it's ended up is:
When the UTC offset representation is absent, date-only forms are interpreted as a UTC time and date-time forms are interpreted as a local time.
Since your string doesn't have a UTC offset (no Z
or +00:00
or similar), and does have a time, it's parsed in local time. (Again, on spec-compliant JavaScript engines¹).
My recommendation is either don't parse date strings with the built-in Date
object, or make sure you always have a timezone indicator on the string if you do.
¹ RobG pointed out that Safari parses new Date("2019-03-06T00:00:00")
as UTC. Sadly, this is a bug in JavaScriptCore, Apple's JavaScript engine. It affects not only Safari, but Chrome on iOS as well (and probably any other iOS browser; I've tested Brave, Opera, and Dolphin), since Chrome has to use JavaScriptCore instead of its usual V8 on iOS because apps can't allocate executable memory, so JIT engines can't be used on iOS. But the V8 team have made an interpreter-only version of V8, so maybe Chrome (and Brave) on iOS will be updated to use that if it's fast enough.
这篇关于date-fns在Safari中返回无效日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!