将日期传递给 Date 对象会导致 IE8 中的变量未定义
我已经在这方面坚持了一段时间了,如果可能的话,我将不胜感激。
基本上,我从 API 收到格式为 yyyy-mm-dd 的日期。除此之外,我希望显示工作日。以下是相关代码:
// jsonDate is in the format yyyy-mm-dd
var splitDate = jsonDate.split("-");
var joinedDate = splitDate.join(",");
var myDate = new Date(joinedDate);
var weekday=new Array(7);
weekday[0]="Sun";
weekday[1]="Mon";
weekday[2]="Tue";
weekday[3]="Wed";
weekday[4]="Thu";
weekday[5]="Fri";
weekday[6]="Sat";
var dayOfTheWeek = weekday[myDate.getDay()];
在 Firefox 中一切正常,但在 IE8 中“dayOfTheWeek”未定义。当我控制台记录它时,IE 开发人员工具还将“myDate”显示为“NaN”。
任何帮助将不胜感激。
I've been stuck on this for quite a while and would appreciate some help if possible.
Basically I receive a date from an API in the format yyyy-mm-dd. Amongst other things, I wish to display the weekday. Here is the relevant code:
// jsonDate is in the format yyyy-mm-dd
var splitDate = jsonDate.split("-");
var joinedDate = splitDate.join(",");
var myDate = new Date(joinedDate);
var weekday=new Array(7);
weekday[0]="Sun";
weekday[1]="Mon";
weekday[2]="Tue";
weekday[3]="Wed";
weekday[4]="Thu";
weekday[5]="Fri";
weekday[6]="Sat";
var dayOfTheWeek = weekday[myDate.getDay()];
Everything works as it should in Firefox, but in IE8 "dayOfTheWeek" is undefined. IE Developer tools also shows "myDate" as "NaN" when I console log it.
Any help would be really appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
以这种方式创建日期:
Date 期望各部分作为单个参数,而不是格式化字符串。
还有一个传递字符串的选项,例如:
但我认为你最好不要使用这个,它可能取决于 javascript 版本,哪种格式会被接受。
Create the date that way:
Date expects the parts as single arguments, not as a formatted string.
There is also a option to pass a string, for example:
but I think you better not use this, it might depend on the javascript-version, which format would be accepted.
dstring('2010-10-21')
dstring('2010-10-21')