I've recently implemented a webview based mobile app for a cloudbased system, and I've come across a frustrating problem.
We store the dates in the database in the following format:
2016-10-11T11:09:00
In the desktop applications it comes up correctly as 2016/10/11 11:09:00.
However, in the mobile app (we use phonegap, so it's HTML + JS based), when I get this date string from the database via a PHP call, and I convert it, using 'new Date("2016-10-11T11:09:00")', it shows up with a timezone conversion, so for instance if I'm in GMT + 2, it will be 2016/10/11 13:09:00.
How can I prevent this?
Whenever I show the dates on the views, I use a date.toString("yyyy/MM/dd HH:mm:ss") formatting.
Also, I'm open to any suggestions, in terms of storing and showing datetime across multiple platforms. I found this yyyy-mm-ddThh:mm:ss formatting to be quite good and videly used, until I bumped into this javascript problem.
(I'm not a javascript or HTML expert, as far as you can tell)
LE: I don't want to convert the date to UTC string, I want the actual Date object, to be consistent with the database value, cause i'm hooking it up to an HTML datetime picker via AngularJS.
To create a Date without the timezone, we called the toISOString method on the Date object and removed the character Z from the ISO string. The Date object shows the exact same time as the one stored in the dateStr variable - 09:35:31 .
There are multiple ways to remove the local time zone from a date: Use the toUTCString() method to convert the date to a string using UTC. Use the toLocaleDateString() method to format the date according to a specific locale. Format the date and time consistently, e.g. as YYYY-MM-DD hh:mm:ss .
JavaScript's internal representation uses the “universal” UTC time but by the time the date/time is displayed, it has probably been localized per the timezone settings on the user's computer. And, indeed, that's the way JavaScript is set up to work.
To remove the T and Z characters from an ISO date in JavaScript, we first need to have the date in ISO format. If you don't have it already, you can convert it to ISO using the toISOString function on a Date object. This will get rid of both T and Z, resulting in "2022-06-22 07:54:52.657".
What is it that you actually want to achieve? If you want to show the timestamps always as they are ignoring timezone information, you might make things quite confusing to the user when those timestamps might show future time to some of the users. For example: someone modifies data and another user in US west coast views the data and sees the timestamp pointing to the future.
Maybe something you wan to use is Data.toUTCString(), that shows the timestamp so that you will have GMT as a timezone. Then it will be clear that to the users that this timestamp is not in their timezone. Also you might want to have timezone information appended to the timestamp string so that the conversions happen correctly. If you your timestamps are in UTC (or Zulu time) you can add "Z" to the end of the string. RFC 2822 Section 3.3 defines that date and time should express local time, but at least with node.js if you leave timezone information out, it seems to express UTC time. So adding the timezone information will make your life easier (would be even better if you would add timezone information in the in the string that you store in your database).
As an example:
var timestamp = "2016-10-11T11:09:00";
var d = new Date(timestamp);
// or
d = new Date(timestamp + "Z");
// Then showing the date
d.toString();
// or without the converting it to user's local timezone
d.toUTCString();
I hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With