ASP.NET MVC Handle Timezones Entity Framework - entity-framework

I like to save the current DateTime.Now for the current user as UTC. For any user the time is than relative to each other ? I used this code snippet to realize UTC for the Entity Framework:
Entity Framework DateTime and UTC
After a look into the database the column for the date has not changed.
09.05.2014 20:21:24
I need to show on the client side the date relative to each user. So when the user created his account in America 14:00 I like to see in Europe 20:00.

First of all you need to use DateTime.UtcNow instead of DateTime.Now. EF will save date as it is, it will not do anything.
For database, it is just a date, whether it is UTC or specific time zone, database has no idea. It is only when we retrieve date from database, before displaying it to local user, we should call ToLocalTime or similar method in client UI library to convert UTC into localtime. ToLocalTime assumes that input is actually UTC.
If you are saving date from client side, for example with AJAX post in JavaScript, JavaScript dates should be serialized as UTC.
Remember that server has no idea from where you are sending date, server cannot convert any date to UTC, client on other hand has complete idea of which country and which timezone it is set at and it can easily convert local time into UTC.

Calling .ToLocalTime() on a DateTime object will convert it to the local time. You should always Save the date as UTC then convert upon display.

Related

ExtJS Bug when selecting date from datefield

I have a:
Field in my store as date with dateFormat set to 'Y-m-d'
Column in my grid (datecolumn) - format set to 'Y-m-d'
Editor on Column (datefield editor) - format set to 'Y-m-d'
Postgresql Table with a date column (Note, not a timestamp or timestamptz, but date)
I have data that comes into the store from Postgresql like:
{
mydatefield: "2021-07-30"
}
Now, the date do dispay is 100% correct in the grid. Problem comes in when I select a new date, through the datefield editor, let's say
2021-07-31
The moment it saves back to the server it passes:
mydatefield: '2021-07-30T22:00:00.000Z'
and the server saves it wrong as '2021-07-30' and the grid refreshes back to 2021-07-30.
We are on South African Standard Time (+2) Do not know if it something to do with Daylight Saving
So, to recap. It does not matter what date I select, it keeps saving a day less than the day I selected.
The date it's saving is in UTC, which is the timezone you ought to be using on your server(s) and database(s) - that's a good thing as it removes timezone related info and allows dates to be localized to any timezone.
When you transmit date values over the wire between the server and the browser, make sure you're using RFC-8701 formatted strings (what you get when you call new Date().toJSON() in JavaScript). Then you won't have to worry about timezone issues, as RFC-8701 dates are always in UTC. The browser will take care of transforming dates to local time - just call new Date(myRfcDateString).
More info: Storing date/times as UTC in database
Use convert method of store to process data for date column before storing it in store data. Then the store data will be submitted to server in proper format.
There was similar question asked, links below -
Question - Datefield in grid editor
Fiddle - https://fiddle.sencha.com/#view/editor&fiddle/2e0a

How to fix dates lagging one day behind in calendar

I'm developing a Clio integration with access to the calendar, but there's been an issue with dates. While the documentation says they expect an ISO-8601 timestamp date, it seems like there's something adding offset to the timezone value in dates being sent to the system.
For example, if I send a date 2018-05-17T23:59:59.999999-04:00 on both start_at and end_at properties when creating a calendar entry for an all day event, the value returned when fetching this entry through the API is 2018-05-17T17:00:00-07:00, which is clearly wrong. Am I missing something here?
The expected result should be something like either 2018-05-17T23:59:59-04:00 or 2018-05-18T03:59:59Z if milliseconds are ignored.
All dates are based on UTC timezone. Could it be that your site/server/script is set to a local timezone and so the dates are off for part of the day?
Try setting your scripting environment to UTC time before making any date/time-based queries.

Ext.Date.format wrong format

I am facing an issue in ExtJS with the Date format.
I'm using Ext.Date.format(value, 'm/d/Y h:i:s a') where the value is a date from the database.
While saving a Date 06/29/2018 12:15:00 am to DB it's storing as 2018-06-26 07:30:00
I am getting the Date from Database as 2018-06-28T18:45:00.000Z when converting to Ext.Date.format it changes to 06/29/2018 12:15:00 am.
I need store the date to be same as UI(06/29/2018 12:15:00 am) in db. Is there anyway?
Thanks
The reason might not be ExtJS itself nor your database, but the fact that your server and browser are on different timezones (i.e. Server in USA and browsing in Europe). I can think of two solutions:
1) Store the timezones in the database along with the dates. The record would look something like this: 2018-06-28T18:45:00.000Z+10:00. Use .NET's DateTimeOffset type in your C# code for this alternative.
2) What you can do instead is to convert all dates to UTC in the server side. Probably calling DateTime.ToUniversalTime() will suffice, but you can see this answer for more details.
Then you can use a library like moment.js which helps a lot with date and time manipulation/formatting to display the dates in the user's local timezone.

Postgres prevent timestamp with timezone conversion

I have a table that I am using to store iso dates with timezones. I realize that dates should "always" be stored as utc but I have an exception to that rule. The timestamps aren't in any way related to the server they are running on. I want to be able to store an iso date like this:
2016-03-06T01:15:52-06:00
And regardless of the time zone of the server or anything else I want the timestamp returned as:
2016-03-06T01:15:52-06:00
Currently if I insert an iso date it automatically converts it to whatever the server timezone is. My above date gets converted to:
2016-03-06 07:15:52+00 (server is utc)
The only thing I can think of is storing the timezone offset in a separate column, storing my date as utc and then converting using the offset column, horribly messy. Surely there is a way to store my date in one column and get it out the way it was originally created?
Your proposed solution is correct. Or more precisely, it is one of several correct implementations. Any of the following would work:
Store the UTC timestamp in one field, store the offset in another.
Store the local timestamp in one field, store the offset in another.
Store the local date in one field, and store a time with time zone in another. (though time with time zone is generally discouraged...)
Store the UTC timestamps in one field and the local timestamp in another.
The easiest by far is the first one, which you already proposed.
I'd avoid against storing timestamps in text fields, as they tend not to be very efficiently searchable.
Also note - if you're coming from a SQL Server background, you might recall its datetimeoffset type, which stores the local datetime and offset in the field, and uses the UTC equivalent during indexing. It's common to think that Postgres and MySQL's timestamp with time zone would have the same behavior, but they don't. They simply use the session time zone to convert to/from UTC. SQL Server has no concept of a session time zone, and thus the discrepancy.
Be sure to read this part of the Postgres docs.

GWT - Obtain Browser Timezone/Timestamp formatting

I send timestamps to my GWT client using GMT/Zulu time, with each string designated as such (ie. 2012-01-19T16:29:18Z, or 4:29pm GMT). How do I show this in the browser in the local timezone? So for me in CST I expect to see 10:29am.
If I do no formatting I get the date as 4:29pm, which I expect, If I use a TimeZone object of TimeZone.createTimeZone(0), I get for some reason 10:29PM (not AM as expected). I suppose I'm supposed to pass in something more meaningful than 0, but how do I obtain the right value for the where the browser is running?
J
You can get the time-zone offset that is configured in the browser using:
Date d = new Date();
d.getTimezoneOffset();
It won't give you the timezone name, I don't think that's possible to get.
Do you send the time as a timestamp, or string? I find the best approach is to send UTC timestamps to the client and then format them to whatever zone I need using DateTimeFormat/TimeZone. But I guess that if you are parsing the date string including the offset, you end up with a UTC timestamp anyway.
If you want the GWT client code to format the time in the browser time zone, you should pass the data from the server to the client as a java.util.Date object.