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?
–
–
–
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
Just if someone else face the same issue and visit this question.
There is a solution here:
https://stackoverflow.com/a/28149561
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
site design / logo © 2020 Stack Exchange Inc; user contributions licensed under cc by-sa 4.0
with attribution required.
rev 2020.2.7.36004