Why does the conversion of new.Date (). ToISOString () change the time?

I insert the date into the database in two different formats.

this is insert as datetime

    var mydate;
    mydate = new Date();
    document.getElementById('clockinhour').value = mydate.toISOString().slice(0, 19).replace('T', ' ');

Output A

2017-06-21 20:14:31 

this is an insert like varchar:

document.getElementById('clocked_in_time').value = Date();

Exit B

Wed Jun 21 2017 16:14:31 GMT-0400 (Eastern Standard Time)

Output B is the correct time, but I need to display output A. What makes time change when converting to an ISOString? How can i fix this?

+4
source share
2 answers

When inserted as a Datetime, your block slicedisables part of the time zone ( Zat the end of the output toISOString):

document.getElementById('clockinhour').value = mydate.toISOString().slice(0, 19).replace('T', ' ');

As pointed out by @RobG in the comments section, toISOStringit should always return a date in UTC ( Zor +00:00) format .

RTFM: " [] UTC, Z",

"", UTC, toISOString.

ISO , : ISO 8601 JavaScript? JavaScript

+1

ISO - . , b GMT-04: 00, 16 , 20

0

Source: https://habr.com/ru/post/1679859/


All Articles