alert(new Date('2010-11-29'));

Chrome, ff在这方面没有问题,但safari会喊“无效日期”。为什么?

编辑:好的,根据下面的评论,我使用了字符串解析,并尝试了这个:

alert(new Date('11-29-2010')); //doesn't work in safari
alert(new Date('29-11-2010')); //doesn't work in safari
alert(new Date('2010-29-11')); //doesn't work in safari

编辑2018年3月22日:似乎人们仍然在这里着陆-今天,我会使用moment或date-fns,然后就不用了。Date-fns是非常痛苦的,而且很轻。


当前回答

正如@nizantz前面提到的,在Safari中使用Date.parse()对我来说不起作用。经过一番研究,我了解到File对象的lastDateModified属性已弃用,Safari不再支持它。使用File对象的lastModified属性解决了我的问题。当然不喜欢在网上发现不好的信息。

感谢所有在这篇文章中做出贡献的人,他们帮助我走上了我需要了解的问题的道路。如果没有这些信息,我可能永远也不会发现我的根本问题。也许这能帮助到和我处境相似的人。

其他回答

正如@nizantz前面提到的,在Safari中使用Date.parse()对我来说不起作用。经过一番研究,我了解到File对象的lastDateModified属性已弃用,Safari不再支持它。使用File对象的lastModified属性解决了我的问题。当然不喜欢在网上发现不好的信息。

感谢所有在这篇文章中做出贡献的人,他们帮助我走上了我需要了解的问题的道路。如果没有这些信息,我可能永远也不会发现我的根本问题。也许这能帮助到和我处境相似的人。

模式yyyy-MM-dd并不是Date构造函数的官方支持格式。Firefox似乎支持它,但别指望其他浏览器也能这么做。

下面是一些受支持的字符串:

; yyyy / MM / dd MM / dd / yyyy MMMM dd, yyyy MMM dd, yyyy

DateJS似乎是解析非标准日期格式的一个很好的库。

编辑:刚刚检查了ECMA-262标准。引自15.9.1.15节:

日期时间字符串格式

ECMAScript defines a string interchange format for date-times based upon a simplification of the ISO 8601 Extended Format. The format is as follows: YYYY-MM-DDTHH:mm:ss.sssZ Where the fields are as follows: YYYY is the decimal digits of the year in the Gregorian calendar. "-" (hyphon) appears literally twice in the string. MM is the month of the year from 01 (January) to 12 (December). DD is the day of the month from 01 to 31. "T" appears literally in the string, to indicate the beginning of the time element. HH is the number of complete hours that have passed since midnight as two decimal digits. ":" (colon) appears literally twice in the string. mm is the number of complete minutes since the start of the hour as two decimal digits. ss is the number of complete seconds since the start of the minute as two decimal digits. "." (dot) appears literally in the string. sss is the number of complete milliseconds since the start of the second as three decimal digits. Both the "." and the milliseconds field may be omitted. Z is the time zone offset specified as "Z" (for UTC) or either "+" or "-" followed by a time expression hh:mm This format includes date-only forms: YYYY YYYY-MM YYYY-MM-DD

它还包括只有时间的表单 附加一个可选的时区偏移量: THH:毫米 THH: mm: ss THH: mm: ss.sss 还包括“约会时间” 可以是上述的任何组合。

所以,YYYY-MM-DD似乎包含在标准中,但出于某种原因,Safari不支持它。

更新:在查看datejs文档后,使用它,你的问题应该用这样的代码来解决:

var myDate1 = Date.parseExact("29-11-2010", "dd-MM-yyyy");
var myDate2 = Date.parseExact("11-29-2010", "MM-dd-yyyy");
var myDate3 = Date.parseExact("2010-11-29", "yyyy-MM-dd");
var myDate4 = Date.parseExact("2010-29-11", "yyyy-dd-MM");

对于使用date-fns的人,我们可以parseISO日期并使用它来格式化

无效的

import _format from 'date-fns/format';

export function formatDate(date: string, format: string): string {
  return _format(new Date(date), format);
}

此函数处理safari抛出无效日期错误。

解决方案 要解决这个问题,我们应该使用:

import _format from 'date-fns/format';
import _parseISO from 'date-fns/parseISO';

export function formatDate(date: string, format: string): string {
  return _format(_parseISO(date), format);
}

我也遇到过类似的问题。Date.Parse("DATESTRING")适用于Chrome (Version 59.0.3071.115),但不适用于Safari (Version 10.1.1 (11603.2.5))

Safari:

Date.parse("2017-01-22 11:57:00")
NaN

铬:

Date.parse("2017-01-22 11:57:00")
1485115020000

对我有效的解决方案是用“T”替换dateString中的空格。(例如:dateString。(/ /g,"T"))

Safari:

Date.parse("2017-01-22T11:57:00")
1485086220000

铬:

Date.parse("2017-01-22T11:57:00")
1485115020000

注意,Safari浏览器的响应比Chrome浏览器的响应少8hrs (28800000ms),因为Safari以本地TZ返回响应(比UTC晚8hrs)

得到相同TZ的两个时间

Safari:

Date.parse("2017-01-22T11:57:00Z")
1485086220000

铬:

Date.parse("2017-01-22T11:57:00Z")
1485086220000

这不是最好的解决方案,尽管我只是捕获错误并返回当前日期。我个人觉得不解决Safari的问题,如果用户想要使用一个该死的不符合标准的浏览器——他们就得忍受这些怪癖。

function safeDate(dateString = "") {
  let date = new Date();
  try {
    if (Date.parse(dateString)) {
      date = new Date(Date.parse(dateString))
    }
  } catch (error) {
    // do nothing.
  }
  return date;
}

我建议让你的后端发送ISO日期。