New Date () doesn't return current time in mozilla

Google exit

  • eventObject.srcElement.getValue () = "2015-06-17T08: 30: 40,000"

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

  • new Date () = Wed Jun 17, 2015 12:53:03 GMT + 0530 (Indian Standard Time)

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

Mozilla exit

  • eventObject.srcElement.getValue () = "2015-06-17T08: 30: 00.000"
  • new Date (eventObject.srcElement.getValue ()) = Date 2015-06-17T03: 00: 00.000Z
  • new Date () = Date 2015-06-17T07: 21: 14.629Z
  • new date (eventObject.srcElement.getValue ()) <= new date () true,

When I try to enter a date using a date picker it does not allow me to select the current date with more than the current time, however it should not allow when I give less than the current time and it should allow more than the current time. (This feature works great in Chrome)

thank

+3


source to share


1 answer


This is unfortunately because the TC-39 committee got a little confused when defining the standard ECMAScript5 date and time format : they based it on ISO-8601, but said that the absence of a timezone indicator on the line defaults to GMT ("Z") ... But in ISO-8601, the time zone indicator indicator means local time.

They fix it in ECMAScript6, and now we're in a nasty environment where some JavaScript engines implement the ES5 spec, even with a bug (like Chrome), and others implement the ES6 spec (like Firefox, starting with this post).

So the only way to reliably parse this date through an object Date

, cross-browser, is to add a timezone indicator to it to make it unambiguous. Here's a function that checks if the string has a timezone as indicator, and if not, adds Z, so it will be treated like GMT and apply 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 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>
      

Run codeHide result


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

+4


source







All Articles