admin管理员组

文章数量:1279083

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:

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:

Share Improve this question asked Jun 22, 2011 at 18:34 Chris DutrowChris Dutrow 50.4k67 gold badges195 silver badges262 bronze badges 6
  • 1 Works fine for me in Chrome running in the console directly on the datejs website – Matt Commented Jun 22, 2011 at 18:36
  • Yeah, that worked for me too, actually – Chris Dutrow Commented Jun 22, 2011 at 18:37
  • Which version of DateJS were you using in the above? – Matt Commented Jun 22, 2011 at 18:38
  • 1 should also point out that "12:00 am" was incorrectly parsed to 12:00 instead of 00:00. – Mike Ruhlin Commented Jun 22, 2011 at 18:41
  • Silly aside: if you think about the literal meaning of "pm" there is no "12:00 pm" - though you can say "1200" in 24-hour format, or "noon"... – nnnnnn Commented Jun 23, 2011 at 0:20
 |  Show 1 more ment

2 Answers 2

Reset to default 16

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

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?

本文标签: javascriptDatejsProblem with 1200 pmStack Overflow