将日期传递给 Date 对象会导致 IE8 中的变量未定义

发布于 2024-09-29 10:42:49 字数 593 浏览 2 评论 0原文

我已经在这方面坚持了一段时间了,如果可能的话,我将不胜感激。

基本上,我从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

对不⑦ 2024-10-06 10:42:49

以这种方式创建日期:

var myDate = new Date(splitDate[0],splitDate[1]-1,splitDate[2]);

Date 期望各部分作为单个参数,而不是格式化字符串。

还有一个传递字符串的选项,例如:

MonthAsString Day , year hours:minutes:seconds

但我认为你最好不要使用这个,它可能取决于 javascript 版本,哪种格式会被接受。

Create the date that way:

var myDate = new Date(splitDate[0],splitDate[1]-1,splitDate[2]);

Date expects the parts as single arguments, not as a formatted string.

There is also a option to pass a string, for example:

MonthAsString Day , year hours:minutes:seconds

but I think you better not use this, it might depend on the javascript-version, which format would be accepted.

安静 2024-10-06 10:42:49
function dstring(str){
    //yyyy-mm-dd
    str= str.split(/\D+/);  
    str[1]-= 1;
    try{
        var d= new Date();
        d.setHours(0,0,0,0);
        d.setFullYear.apply(d, str);
    }
    catch(er){
        return 'Bad date-'+str;
    }
    return d;
}

dstring('2010-10-21')

/*  returned value: (Date)
Thu Oct 21 2010 00:00:00 GMT-0400 (Eastern Daylight Time)
*/
function dstring(str){
    //yyyy-mm-dd
    str= str.split(/\D+/);  
    str[1]-= 1;
    try{
        var d= new Date();
        d.setHours(0,0,0,0);
        d.setFullYear.apply(d, str);
    }
    catch(er){
        return 'Bad date-'+str;
    }
    return d;
}

dstring('2010-10-21')

/*  returned value: (Date)
Thu Oct 21 2010 00:00:00 GMT-0400 (Eastern Daylight Time)
*/
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文