21

I'm inserting a date in a database in two different format.

this is inserting 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 inserting as varchar :

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

Output 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 causes the time to change when converted toISOString? How can I fix this?

Sebastian Farham
  • 797
  • 2
  • 8
  • 24
  • `Output B is the correct time but I need to display output A`, do you mean display in the format that output A is? – Patrick Roberts Jun 22 '17 at 03:51
  • Does this mean you would like to display the date time in EST format and not ISO format? – Jaya Jun 22 '17 at 03:51
  • 3
    [*RTFM*](http://www.ecma-international.org/ecma-262/7.0/index.html#sec-date.prototype.toisostring): "*The time zone is always UTC, denoted by the suffix Z*", – RobG Jun 22 '17 at 03:55
  • Question is repeat: https://stackoverflow.com/questions/21300956/whats-the-difference-between-datetime-in-iso-8601-and-utc-formats-in-javascript – Pratik Ghag Jun 22 '17 at 04:30
  • Possible duplicate of [What's the difference between datetime in ISO 8601 and UTC formats in javascript?](https://stackoverflow.com/questions/21300956/whats-the-difference-between-datetime-in-iso-8601-and-utc-formats-in-javascript) – Pratik Ghag Jun 22 '17 at 04:31
  • @PatrickRoberts: I use datetime output A to make calculations in the database. It is used to calculate timediff between two dates and show user total time they have worked. Output B is proper time that's what I mean, it's 16h00(4pm) not 20h00(8pm). – Sebastian Farham Jun 22 '17 at 21:05
  • @Jaya: Somehow yes. Whatever time they are in. – Sebastian Farham Jun 22 '17 at 21:06

3 Answers3

11

In your this is inserting as Datetime block your slice are stripping of the timezone part (the Z at the end of toISOString output):

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

As pointed out by @RobG in the comments section, toISOString should always return the date in UTC (Z or +00:00).

RTFM: "The time zone [offset] is always UTC, denoted by the suffix Z",

The time "changes" because it is converted to UTC when you calls toISOString.

If you want to get ISO date in your timezone, you should take a look in these two questions: How to ISO 8601 format a Date with Timezone Offset in JavaScript? and How to format a JavaScript date

Dinei
  • 2,451
  • 3
  • 25
  • 49
5

ISO time is time zone free. You'll notice with b you have time zone GMT-04:00 if you add those four hours to the 16 hours in the Date, you get 20

ControlAltDel
  • 28,815
  • 6
  • 42
  • 68
3

Just if someone else face the same issue and visit this question.

There is a solution here: https://stackoverflow.com/a/28149561

Nafis
  • 521
  • 10
  • 26