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是非常痛苦的,而且很轻。


当前回答

我用矩来解决这个问题。 例如

var startDate = moment('2015-07-06 08:00', 'YYYY-MM-DD HH:mm').toDate();

其他回答

这不是最好的解决方案,尽管我只是捕获错误并返回当前日期。我个人觉得不解决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日期。

最好的方法是使用以下格式:

new Date(year, month, day, hours, minutes, seconds, milliseconds)
var d = new Date(2018, 11, 24, 10, 33, 30, 0);

这在所有浏览器中都是受支持的,不会给您带来任何问题。 请注意,月份是从0到11。

模式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");

将字符串转换为日期格式(你必须知道服务器时区)

new Date('2015-06-16 11:00:00'.replace(/\s+/g, 'T').concat('.000+08:00')).getTime()  

where +08:00 =时区从服务器

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

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