new Date() is not returning the current time in mozilla

194 views Asked by At

Google Output

  1. eventObject.srcElement.getValue()="2015-06-17T08:30:40.000"

  2. new Date(eventObject.srcElement.getValue())= Wed Jun 17 2015 14:00:40 GMT+0530 (India Standard Time)

  3. new Date()= Wed Jun 17 2015 12:53:03 GMT+0530 (India Standard Time)

  4. new Date(eventObject.srcElement.getValue()) <= new Date() false

Mozilla Output

  1. eventObject.srcElement.getValue()= "2015-06-17T08:30:00.000"
  2. new Date(eventObject.srcElement.getValue())= Date 2015-06-17T03:00:00.000Z
  3. new Date()= Date 2015-06-17T07:21:14.629Z
  4. new Date(eventObject.srcElement.getValue()) <= new Date() true

When I am trying to enter date through date picker its not allowing me to select current date with more than current time, however it should not allow when i give less than current time and it should allow more than current time. (This functionality is working fine in Chrome)

Thanks

1

There are 1 answers

0
T.J. Crowder On

This is, unfortunately, because the TC-39 committee messed up a bit when defining ECMAScript5's standard date/time format: They based it on ISO-8601, but said that having no timezone indicator on the string defaulted to GMT ("Z"). But in ISO-8601, no timezone indicator means local time.

They're fixing this in ECMAScript6, and so now we're in the unpleasant middle where some JavaScript engines implement the ES5 specification even with the error (Chrome, for example, as of this writing), and others implement the ES6 specification (Firefox, for example, as of this writing).

So the only way you can reliably parse that date via the Date object, cross-browser, is to add a timezone indicator to it so it's unambiguous. Here's a function that checks if the string has as timezone indicator and, if not, adds a Z so it will be treated as GMT and applies the timezone offset to make it local time again:

// Note: I'm not at all sure that does the right thing during the weird hour
// at the end of daylight savings time. I think it gets the *beginning*
// of DST right, and it's fine the rest of the time (no pun).
Date.fromSimpleISO = function(str) {
    var dt;

    if (str.substr(-1) === "Z" || /[+\-]\d{2}:\d{2}$/.test(str)) {
        // It has as timezone indicator, just pass it on
        dt = new Date(str);
    } else {
        // It should be local time: Parse as GMT, then apply offset
        dt = new Date(str + "Z");
        dt.setMinutes(dt.getMinutes() + dt.getTimezoneOffset());
    }
    return dt;
};
function test(str) {
  snippet.log(str + " => " + Date.fromSimpleISO(str));
}
test("2015-06-17T08:30:40.000");
test("2015-06-17T08:30:40.000Z");
test("2015-06-17T08:30:40.000+05:30");
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>

Or of course, parse the string yourself, as the format is really simple. Or use a library like MomentJS to do it for you.