String para Date

Asked

Viewed 305 times

6

I’m creating some parsers and was doing some tests to develop Date’s.

This code:

value = '2015-12-31 23:16:00'
value = value.replace(/(\d{4}-\d{2}-\d{2}) ?(\d{2}:\d{2})?(:\d{2})?.*/, '$1T$2$3');
value = value.replace(/T$/, '');
value = value.replace(/(:\d{2})$/, '$1Z');

value = new Date(value)

runs correctly on Firefox, returning Date 2015-12-31T23:16:00.000Z, however in Chrome it displays Thu Dec 31 2015 21:16:00 GMT-0200 (BRST)

In addition to displaying differently, Chrome killed two hours of the original time.

Would anyone know how to turn this around?

  • 1

    without the 'Z' worked properly here in my Chrome, maybe I’ll give you a little something http://stackoverflow.com/questions/15109894/new-date-works-differently-in-chrome-and-firefox

  • 1

    @Maiconcarraro actually I had already found this reference, from Z at the end, and I’m applying it, if you comment on mine that has value = new Date(value) value displayed in the sera console, "2015-12-31T23:16:00Z", however still remains a difference of two hours in the Chrome

  • 2

    By my tests there is no problem at the time, it is only their display that is divergent in the browsers. In both running the getUTCHours(), returned me 23. As in other functions.

1 answer

0


As @Guilherme Lautert said in the comment, in both cases returns the same value, just presenting it differently. What changes is only the time zone that was used, and GMT-0200 indicates two hours less than the current time in the Greenwich meridian. Example: http://www.donathan.com/dave/m0200.html.

Try to use

var horarioAtual = date.toLocaleTimeString();

to check. It is likely to return the correct value in both cases.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.