javascript IE8:对象不支持此属性或方法(日期函数)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18068517/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 10:38:13  来源:igfitidea点击:

IE8: Object Doesn't Support This Property or Method (Date function)

javascriptjqueryvalidationinternet-explorer-8jquery-validate

提问by Neil

I'm getting an error that only appears on the great IE8, it points to the following function, specifically the line: return (expDate.getTime() > Date.now());

我收到一个只出现在 IE8 上的错误,它指向以下函数,特别是该行: return (expDate.getTime() > Date.now());

$.validator.addMethod("checkDocExpiry",function(value) {
    var driverLicExp = ($('#drivers-license-expiration').val()) ? $('#drivers-license-expiration').val() : '';
    if (driverLicExp != ''){
        var expDate = new Date(driverLicExp);
        return (expDate.getTime() > Date.now());
    }else{
        return (true);
    }
}, "Your driver's license has expired.");

I'm not sure what would cause this, I am fairly new to developing for older browsers. This runs fine in FF, IE10, Chrome, Safari.

我不确定是什么导致了这种情况,我对旧浏览器的开发还很陌生。这在 FF、IE10、Chrome、Safari 中运行良好。

Any help would be much appreciated.

任何帮助将非常感激。

Thanks

谢谢

回答by Jason P

Looks like Date.now()isn't supported in IE8 (see the table at the bottom):

看起来Date.now()在 IE8 中不受支持(请参阅底部的表格):

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/now

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/now

new Date()should get you a date object with the current date.

new Date()应该给你一个带有当前日期的日期对象。

回答by Paul S.

Shim using the fact valueOfa Dateis ms..

Shim 使用事实valueOfa Datems..

if (!Date.now) Date.now = function () {return +new Date();};

回答by Neil

IE 8 does not support Date.now. Implement it as :

IE 8 不支持 Date.now。将其实现为:

if(!Date.now) { Date.now = function(){ return new Date().getTime();};}

回答by SLaks

My psychic debugging skills tell me that you're using jQuery 2.0, which does not support IE8.

我的通灵调试技巧告诉我,您使用的是不支持 IE8 的 jQuery 2.0。

You need to use 1.10.

您需要使用 1.10。