In my application there is a requirement to display date times in different time zones. All these timestamps are stored in the database in UTC.
If I send the UTC date time to client and display using JavaScript, it automatically converts it to the client's time zone.
For example:
//the string is what I receive from the server
var date = new Date('2019-05-03T09:30:00.000+05:30');
document.write(date.toLocaleString("en-US"));
displays 5/2/2019, 11:00:00 PM in the browser which is in the Central Time Zone.
What if I do not want this automatic change, and just want to display the date and time that I received from the server?
I know I can get the offset from the date object, but then I have to do additional processing to display the same value that I already got from the server!
Related
I need to change my timezone to India timezone, but my shared server database showing some junk timezone I tried all the possibility but I can't get the India time zone.
I am using PHP Codeigniter
I Have a contact us page on my website, which saves all the send inquiry details to my mail and database. on that is showing some junk time on it
please help me out of this issue
In the case of shared hosting you would need to contact support guys or go for a live chat and they can change the default time zone for you.
You can use date_default_timezone_set() which sets a default timezone.
Please give a look here
OR
convert all your times to UTC on server side and just work with UTC instead
EDITED ANSWER
What you can do is :
Store your date time in DB in UTC, and while showing You can show Date time By getting current timezone of India.
// create a $dt object with the UTC timezone
$dt = new DateTime('2016-12-12 12:12:12', new DateTimeZone('UTC'));
// change the timezone of the object without changing it's time
$dt->setTimezone(new DateTimeZone('Asia/Kolkata'));
// format the datetime
$dt->format('Y-m-d H:i:s T');
I am using NodeJS/javascript to determine the current date which has the time zone of the system. Now i want to insert this date into MySQL with UTC+0. After that I want to select this date with NodeJS/javascript from MySQL again with UTC+0 and insert it now into MS SQL where I want it to be stored again as UTC+0. At the end I select with NodeJS/javascript again, but this time from the MS SQL and also I want the date this time to be in the timezone which is used right now in the system wherever I am in the future.
Here's an example:
For 01.01.2018 15:00:00 UTC+1(German winter time) I want it to be stored in both databases with UTC+0. When summer comes and the time zone of my system changes to UTC+2(German summer time) or I move to another country with e.g. UTC-5 I want to get the date from MS SQL to be converted into the time zone my system uses automaticly.
So the questions are:
How can I insert my date from NodeJS with the systems time zone to MySQL with UTC+0?
How can I select this date, so that I can insert it with NodeJS into the MS SL and it's UTC+0 there too?
How can I select the date from MS SQL with NodeJS so that it has the systems time zone again?
Additional info:
I have two programs in NodeJS communicating with each other. the first one (client) is inserting a date into his MySQL database and sends this to date also to the server application. the reason why he's inserting it into his own MySQL database is for synchronisation reasons when the server is not running, to get them another time. When the server receives the dates from the clients, those should be stored in the servers MS SQL database. admins of the server are able to export those dates into Excel whenever they want. but the dates have to be converted from the date in the MS SQL (UTC+0) into the servers system time zone, so that in the excel file the date is in the systems time zone instead of UTC+0
I think there are some optimizations which can be made across this solution, but I'll answer your questions first.
How can I insert my date from NodeJS with the systems time zone to MySQL with UTC+0?
You get the system time zone using new Date() - to convert that to UTC format which MySQL would understand you can do new Date().toISOString()
To store this value in MySQL you need a DateTime field. Just inserting that value should work, but if it doesn't just parse it for it to be a date.
How can I select this date, so that I can insert it with NodeJS into
the MS SQL and it's UTC+0 there too?
Well you've inserted it in a standard format which is always UTC+0. You just select it and insert it the same.
How can I select the date from MS SQL with NodeJS so that it has the
systems time zone again?
So - you have a DateTime field in MS SQL which you'll be querying through Node.JS - the field is in UTC+0 and you want to get that time accordingly for your server's timezone?
Well - if you do new Date().getTimezoneOffset() that should give you your timezone offset. So it should become something like this:
var date = MSSQLWrapper.someOperation('select d_field from ...');
var offset = new Date().getTimezoneOffset(); // Your offset
date.setHours(date.getHours() + offset); // Add your offset
console.log(date) // your aligned date.
Edit
If you want to change the timezone on selection from SQL you can use AT TIME ZONE
SELECT your_date from your_table
AT TIME ZONE 'Eastern Standard Time' AS my_new_date
You can pass the timezone name in your query from your server side.
or alternatively you can check out TODATETIMEOFFSET
This question also has some good examples: Convert datetime value from one timezone to UTC timezone using sql query
Use moment js and its UTC feature.
This way you will not need to handle the timezones.
Greetings
Dominik
The script timezone is set to Etc/GMT
I am accepting a date (using a datetime-local yyyy-MM-ddTHH:mm) from user to invoke a trigger function.
var at = moment(mydate); // say user sets mydate (local IST) as 2017-06-21T14:31
ScriptApp.newTrigger('onClock').timeBased().at(new Date(at)).inTimezone(CalendarApp.getDefaultCalendar().getTimeZone()).create();
this will invoke the trigger at 2017-06-21 20:01 local time IST
How do I make a code to invoke trigger at 2017-06-21T14:31 local time?
The inTimezone(timezone) method only accepts valid "long format" JODA time zones.
JODA.org
You are already getting the default calendar time zone:
CalendarApp.getDefaultCalendar().getTimeZone()
which returns the "long format" JODA type time zone, and that is what the inTimezone() method needs as it's parameter.
So, you don't need to get the 2017-06-21T14:31 information from the user, UNLESS their default calendar time zone is NOT the same as their local time. If your situation, is that there is a difference between the default calendar time zone, and their local time on their computer; and you want to account for that difference, then you do need to get the local time zone.
If you really do need to get a time zone that is different from the default calendar time zone, then either the user needs to select a time zone, or you can try to get it some other way.
In your question, you stated that the time you got is: 2017-06-21T14:31
That local time 2017-06-21T14:31 doesn't have time zone information in it. If it had the offset, then you could use the offset to work back to the "long format" time zone.
You could have a drop down list for the user to choose from, with values that are valid "long format" JODA time zones.
<select>
<option value="Etc/GMT+12">GMT +12</option>
<option value="Etc/GMT+11">GMT +11</option>
</select>
If you really do need to get the users local time zone from client side code, see the following Stack Overflow answer:
Stack Overflow answer - Getting the client's timezone in JavaScript
var offset = new Date().getTimezoneOffset();
console.log(offset);
Then you will need to convert the offset to a value "long format" time zone. You can do that by having a list "hard coded" into your code, and do a "look up" or construct a "Etc/GMT+##" time zone with code. The offset is the opposite of the GMT number. A standard offset of minus 12 is +12 GMT
Offset --- Long Format
-12:00 --- Etc/GMT+12
I have a website where dates are sent to and from the server using JSON.stringify(). If the client writes a comment it is sent to the server with a date string, which looks like this
"2013-09-21T04:00:00.000Z"
When the server gets the string I create a new date from the string
var server_date = new Date("2013-09-21T04:00:00.000Z");
This date variable is stored in a MongoDB. But when the client reloads the page the date string the client receives looks like
"2013-09-21T00:00:00.000Z"
And when I make a new date from this string on the client
var client_date = new Date("2013-09-21T00:00:00.000Z");
the date object is incorrect. Somehow when I set the client_date the date is set to 9/20/2013 rather than 9/21/2013. When running the server on my local machine this was never a problem. Currently the server is running on AWS so I'm assuming that this is somehow messing with the dates? But I don't understand how the client_date variable is set one day before the date specified in the string, regardless of the hours-min-seconds. How can I format my dates to fix this problem? Thanks!
Z means Universal Time Coordinated (UTC) or Greenwich Mean Time (GMT). Therefore, when it's 2013-09-21T00:00:00.000Z in Greenwich it's still the 20th of September west all the way to where your Client is.
When passing a date in javascript, that has a 00:00 time, to a webservice that is using linq to sql to access the DB, on some clients browsers, after the save, the date that was passed to the datetime column in the db now has a time that is no longer 00:00, some cases it is hours before midnight so the date is moved into the previous days date, other times it is hours before.
How do we always make the date save and not the time to the db?
This is using asp.net webforms and c# and asmx webservices and most queries are compiled.
It depends on the details of how the date is being encoded. At a high level though, you have to:
Make sure the timezone doesn't get wiped out when it's sent from client to server. That means, send it as either a date-only string, or as a date with the timezone preserved; not as a UTC date.
Make use of DateTimeKind (UTC or local) and/or DateTimeOffset on the server to ensure it is properly represented whenever you're sending/receiving to the client/database.
See also here: Linq to SQL DateTime values are local (Kind=Unspecified) - How do I make it UTC?
I'm guessing that the date and time on the client is important. In that case how about converting the date-time info to iso format before sending it to the server and also send the client's timezone offset as well.
var d = new Date
d.toISOString() // 2012-05-05T22:14:35.506Z
// or maybe jus
d.getTime() // milliseconds since jan 1st 1970 or thereabouts
d.getTimezoneOffset() // Timezone offset in minutes from UTC
This way you get the UTC date and time and the timezone offset as well, that is how many minutes UCT time differs from local time. For example Norway would have a negative offset (UTC-Norwegian time = negative).