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).
Related
From what I understand, when you store a date:
const date = firebase.firestore.Timestamp.fromDate(new Date())
You are storing a date object in the local timezone. However, when I later read this date from Firestore using:
doc.data().date.toDate()
This gives me the date in UTC, instead of the local timezone. Is this the expected behaviour? Am I understanding this correctly? It seems like it would make more sense to retrieve the date in local time since the date you stored is in local time.
When you store a date (in Firestore) with
firebase.firestore.Timestamp.fromDate(new Date()), you are storing a
date object in the local timezone.
This is actually not correct: All timestamps in Firestore are saved in UTC. The timezone of the user who saved the timestamp is not saved with the timestamp value.
Is this the expected behaviour?
Yes, storing the value in UTC gives the maximum flexibility for the developer to display it as desired, depending on his specific requirements.
As a matter of fact, it's up to the developer to display it with the timezone he wants when displaying it in a frontend. For example, the Firebase console converts the UTC date the timezone to which your computer is configured.
I will put events on my website and instead of just putting the time of the even in only my timezone, I want it to be automatically converted to the visitor's timezone, maybe from his browser's time or whatever?
Let's say an event starts at 8:00PM here in Europe, but I want it to be shown in the right timezone to someone visiting from Asia. Isn't this possible based on the user's browser or their device's default time?
How can I do that using JS?
If you have the Time provided as UTC (consider the Date.now() method, with Date object as wrapper), and mostly time would be stored as UTC.
a simple .toString() will provide you with a Local Time String.
while a .toUTCString() will give back UTC Time
// Create date object from datetime string
var date = new Date(Date.now());
//remember Date.now() stores date as milliseconds passed from Unix epoch. Date() converts it into "UTC String"
// Coverting to local datetime
console.log(date.toString());
// Coverting local datetime back to UTC
console.log(date.toUTCString()); // Tue, 13 Jan 2021 08:46:30 GMT
The time should be stored without timezone, like UTC.
(new Date()).toUTCString()
And the other user open the browser in another timezone, it will be convert to local timezone automatically
new Date(utc_time_string)
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
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.