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


当前回答

在我的情况下,它不是格式,这是因为在我的后端Node.js模型中,我将数据库变量定义为字符串而不是日期。

我的后端节点数据库模型说:

starttime:{
  type: String,
}

而不是正确的:

 starttime:{
    type: Date,
  }

其他回答

模式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(year, month, day, hours, minutes, seconds, milliseconds)
var d = new Date(2018, 11, 24, 10, 33, 30, 0);

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

使用'mm/dd/yyyy'格式。例如:- new Date('02/28/2015')。它在所有浏览器中都运行良好。

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

我也有同样的问题。然后我用了moment. js .问题消失了。

当从字符串创建moment时,我们首先检查字符串是否 匹配已知的ISO 8601格式,然后返回到新的日期(字符串)如果 找不到已知格式。 警告:浏览器对解析字符串的支持不一致。因为 没有关于应该支持哪种格式、支持什么格式的规范 在某些浏览器中的工作将不能在其他浏览器中工作。 为了在解析除ISO 8601字符串以外的任何字符串时得到一致的结果, 你应该使用字符串+格式。

e.g.

var date= moment(String);