Datejs - Problem with 12:00 pm

2019-04-28 22:26发布

I really have no idea what I'm doing wrong here. I can't get Datejs to properly parse "12:00 pm" however, it seems to work fine on other dates. Below is a clip from the Firefox debugger:

enter image description here

2条回答
ら.Afraid
2楼-- · 2019-04-28 22:47

Try wrapping the code in an IIFE.

<!DOCTYPE html>
<html>
    <body>
        <input type=text id=d onkeyup="parsedate()">
        </input>
        <br>
        <span id=output></span>
        <script type="text/javascript" src="../../../static/js/date.js"></script>
        <script>
            ( function() {
                    parsedate = function() {
                        var input = document.getElementById('d').value;
                        var output = document.getElementById('output');
                        var d = Date.parse(input);
                        if (d !== null) {
                            output.innerHTML = d.toString();
                        } else {
                            output.innerHTML = "------"
                        }
                    }
                }());
        </script>
    </body>
</html>

The IIFE being

(function(){
    //code
}());

What I'm curious about is why FireFox behaves this way. I know they added security updates a few years back that prevent you from overwriting Date.prototype functions, but why is an IIFE capable of accessing this scope?

查看更多
欢心
3楼-- · 2019-04-28 22:56

Download the latest version of Datejs from SVN not the version in the "download" section.

查看更多
登录 后发表回答