I have a table in my database that holds events. The events have a start time and end time. But when they are inserted into the database the time the user inputed is converted to UTC time. So for me, my time offset is -4 hours from UTC time and if I input the start time as 7pm (19:00:00) that is converted to 23:00:00 (UTC) when added to the database.
The Problem
I want the user to be able to select all events at and after 7pm until the end of the day, but If I do the query: WHERE time > 23:00:00 it will only retrieve event where the start time is between 7:00pm and 7:59pm. At 8:00pm the UTC time in the database is 00:00:00 which is less than 23:00:00.
The field type that the start time is stored in is time
I use javascript to get the time-offset and input, and php for mySQL. Any ideas on how to solve this using those languages or SQL?
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_convert-tz
When you pass in your local time, you can wrap it in this conversion function to convert it to UTC time. This is a mysql function, so all the information transform can happen on the server.
Related
I have a database with date as the Id and im going to select the date/id of the last 5 days from my postgresql database to browser using command bellow and send it to the client side using res.send on node/express
SELECT * FROM ratings ORDER BY rating_id DESC LIMIT 5
but if i console log it, it always less 1 day behind, 2021-04-02 even not in the database
i have done parsed it from the client side, but what i got is only the last 4 days, 2021-04-03 is missing
This is because your data field has a timezone component. So my question is: do you need store the timezone component in your dates?
If yes. So when you format your time you need take it in consideration. The browser automatically go to give you the date in the browser time zone, for that may be you can get a day more or less.
If no, you can format the date without take the timezone component.
PD: you should store your date values as UTC date in your DB always.
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!
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
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).