When I send a DateTimeOffset value to an Azure Mobile App inside an object property it changes it to UTC, but I want to keep the TimeZone.
Both client and server JsonSerializerSettings are set to RoundtripKind by default but seems that this is not working.
How can I keep the TimeZonewhen working with DateTimeOffset properties?
When you send DateTimeOffset, you can pass the time zone information in TimeSpan.
You may go through this article for more information.
Also, you may try converting back to Time Zone from UTC again.
For reference here is the article which you can go through.
Related
I have a problem where I need to handle changes in the timezones and changes in the days within the same timezone while saving and then retrieving the records in the database. The two solutions that I could think of yet are, use UTC+0 timezone on the server, OR, send timezone of client each time while saving the record and then use that timezone value while retrieving the record. The former one can handle changes in the timezones of the client but does not handle the problem where client wants to query all the records of a particular day because the change in day won't be at the same time for the server and the client. The latter solution handles the problem of difference in the day change time but does not handle change in the timezone of the same client. What other solution could be there which can handle both the problems?
My asp core 2.0 web api application stores DateTime in UTC format. I have user TimeZone information available in every request through user preference settings (stored in cookies).
The problem is that I need to return to user DateTime in his TimeZone instead of UTC. Do we have some build in utilities to do so?
Currently I follow the next flow:
User sends request with his preferences -> I retrieve his TimeZone
Process whatever request logic
In OnActionExecuting filter convert all response date time to appropriate TimeZone.
This approach seems to be very clumsy. I hope there is a better way to address such kind of issues. Please, suggest
I thought this was the correct way to do this, but apparently it is not.
I write a date to an SQL Server database using the default of GetUtcDate().
I read it back in ASP.NET and display it to the user with theTime.ToLocalTime().
(It's already in UTC, so I need to convert it to localtime, NOT into UTC time again.)
Wouldn't that make the date/time display correctly any where in the world?
But it seems to be 1 hour off when:
The SQL Server is in 1 time zone (might be... or not be in a DST area or time of year).
The webserver running the ASP.NET is in a different timezone (it also may or may not be DST there).
The local user viewing the date/time is also in a totally different timezone (also with or without DST there).
(I'm not sure if it matters, but the webserver only has .net v1.1 and will NEVER be upgraded to v2 or anything else.)
Storing UTC values using GETUTCDATE is fine. Retrieving UTC values into a DateTime object on your web server is fine also. Calling ToLocalTime is not.
When you use ToLocalTime, you are using the local time zone of the machine where the code is running. In this case - your web server.
The web server has absolutely no knowledge of the timezone of your user. You have to manage that yourself in your application logic. Since you are stuck on .Net 1.1, you have no easy way to manage this server side. You'll have to take a client-side JavaScript approach:
Pass the UTC value to the client in a non-displaying manner. For example, an XHR call, or inline script.
Preferably, use the ISO-8601 format, such as 2014-06-10T09:30:00.000Z. You can get this from your DateTime object by using .ToString("o").
On the client, if you're using newer web browsers, you can pass this string directly into the constructor of a JavaScript Date object. However, you may prefer to use a more robust library for browser compatibility and enhanced formatting capabilities. I recommend moment.js.
By passing the UTC value to the browser, you've delegated the work of figuring out the user's time zone. Calling any of the output methods on the Date or moment objects will show the time converted to the user's time zone automatically.
There are plenty of server-side approaches, but they would require you update to a more reasonable version of the .NET Framework.
TimeZoneInfo and Noda Time work on .NET 3.5 and greater.
TZ4Net works on .NET 2.0
With any of the server-side mechanisms, you would need to change your application to:
Ask the user for their time zone in some settings part of your application.
Apply that time zone to the UTC date before delivering a string to the web browser.
Also, you should recognize that .NET 1.1 was released in 2003, and is way past its lifecycle end date. You should review the chart here and consider upgrading.
I am working on an application whose targeted audience is located throughout the globe. My development platform is ASP.Net/Sql Server both are currently hosted on same dediacated server at present.
There could be possibility that Sql Server may be moved into different server and web application to be hosted on different server. Time zone may be different for both the servers.
Currently we are into development and need to handle Universal Date time concept rather than fighting at later stages.
Our database currently is structured with following type of columns for datetime.
DateTime(mm/dd/yyyy hh:MM:ss), SmallDateTime(holds date part only mm/dd/yyyy 12:00:00), Varchar(holds hhMMss in 24 hour format only)
Currently we are saving GetDate() of Sql Server, DateTime.Now of C# i.e. ASP.NET, javascript datetime and any other date directly. No UTC conversion or nothing like that is applicable at the moment. We are neither storing any TimeZone or any offset at the moment.
How do I handle DateTime related issues when
Calling Stored procedure to Save DateTime from UI to Database
Display data from Stored procedure to UI
Display data from ASP.NET Server to Client Browser
Save data from Client Browser to ASP.NET Server
Please suggest some code examples for each of the cases along with Daylight Saving
I work in the company that has business all over the world and we store all dates in UTC.
Basically, in your sql you have to use GETUTCDATE() function instead of GETDATE()
and in C# you use DateTime.UtcNow instead of DateTime.Now.
Database fields could be standard datetime
In your UI you can display date in yyyy/MM/dd format if you don't know or cannot figure out client's local settings.
I have encountered a weird problem with asp.net profiles which is about the value in LastUpdatedDate column in aspnet_Porfile table of asp.net profiles and membership tables.
I am in a country with currently one hour daylight saving. The current time of my local machine is one hour behind what gets saved in the above column and I am totally working locally so there is no database or web server datetime coming through.
Have someone else experienced the same issue with asp.net profiles? It seems it ignores daylight saving settings.
Most ASP.NET 'extras' use UTC which is not affected by DST.
Datetime.ToLocalTime should do the conversion depending on how you create the Datetime you get from the DB