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.
Related
My server's machine timezone is in HST format. When I try to get the timezone by using HTTP request in JavaScript, it is giving me in UTC format. Is there any way to get the server's timezone.
A few things:
Never rely on the server's time zone to be set to anything in particular. It can easily be changed, or you may simply want to move your server somewhere else. Either should not affect your data.
The HTTP header will only give you the time in UTC/GMT. This part of the HTTP specification, in RFC7231 section 7.1.1.1 and 7.1.1.2.
The client knows nothing about the server's time zone, unless you specifically go out of your way to send it yourself. Due to the previous two points, this should not be required anyway, or should be used in very rare circumstances.
The server knows nothing about the client time zone either. If you want to display the value of the server's clock in the client's local time, you have two options:
Send the UTC time to the client, and use JavaScript to convert from UTC to Local time. The JavaScript Date object is sufficient for this, but you may also find libraries like Moment.js and others useful.
Determine the client's local time zone by some other means, either by asking the user, or by guessing. See this answer for more detail. Once you have the time zone ID (such as America/Los_Angeles, etc.) use this on the server-side. This approach is only useful if you have a lot of date/time manipulation to do on the server-side. If you are simply converting to local time for display, prefer option 1.
I have an Azure website that have datetime set to GMT + 0. All of my users are located in GMT +2.
Is there a simple way how to set the time offset globaly on application startup so all the DateTime values will be rendered with the correct offset?
It depends on what your programming language provides. For example, in PHP, you can use date_default_timezone_set. However, in .NET and many other languages, there is no way to change the default time zone - it will always use the local time zone of the machine it's running on.
It's good that Azure sets it's time zones to UTC. It's really not good for a server application or web site to depend on the time zone settings of the computer it's running on. You should simply rely on the features of the language to write your application such that you are in control of the time zone functionality. For example, in .Net, you can use TimeZoneInfo to convert times to any time zone you wish.
Have you tried DateTime.ToLocalTime?
When using TimeZoneInfo.Local.Id on MonoTouch, I get CET (for Central European Time). On ASP.NET, I get Central Europe Standard Time.
I need to transfer the time zone from the iOS device (MonoTouch) to my ASP.NET web service. It is not sufficient to just transfer the time zone difference (UTC +1), since this does not respect the country-specific Daylight Saving Time settings.
What's the best way to get the iPhone's time zone to my web service?
There's more than one data source for time zones inside MonoTouch.
One comes from the .NET base class library (BCL), System.TimeZoneInfo and it's meant to be compatible (it's structure, not the data) with Microsoft .NET implementation. It has two disadvantages.
it's not 100% compatible with MS data (different data source were used);
it cannot be updated without re-building your application with an updated MonoTouch;
The other one is MonoTouch.Foundation.NSTimeZone which are bindings to iOS API. The API is a bit different but the data it returns match what other iOS applications will uses and it will be updated (with iOS updates).
What's the best way to get the iPhone's time zone to my web service?
IMO MonoTouch.Foundation.NSTimeZone is better suited for the reasons above.
It is not sufficient to just transfer the time zone difference (UTC +1), since this does not respect the country-specific Daylight Saving Time settings.
As for your offset to UTC you can get the daylight specific offset (in seconds) with:
var dlo = NSTimeZone.LocalTimeZone.DaylightSavingTimeOffset (NSDate.Now);
Since the devices are different - I would look at deriving a mapping between the two.
Probably your Monotouch application should do that - for each TimeZoneInfo Id listed in the available timezones, store a mapping to the Id for the same TimeZoneInfo on your servers.
Also, be aware that timezones on the server are driven by windows update and might not necessarily be the same on every web server (if you have a farm) - as I discovered to my significnat annoyance the other day on one of my web apps. For more information: http://support.microsoft.com/gp/cp_dst
TimeZoneInfo on Windows uses Windows time zone IDs - whereas presumably on MonoTouch it'll be using whatever the iOS IDs are (although I'm slightly surprised that you get CET rather than a zoneinfo name).
Even if you map between the two, I wouldn't always expect to get the same results. Windows will update the time zone information at different times to iOS. You should either perform all time zone calculations on a single system, or use your own "embedded" time zone implementation in both places to ensure consistency. My Noda Time project may work here, but I've never tried using it in MonoTouch.
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.
Does ColdFusion 8 have any built-in support for handling timezones? We'd like our users to be able to choose a timezone and then have all dates/times on the site adjusted to their locale. Currently we're asking users to set an offset from server time, and it's a headache because they have to go in a couple times each year and manually adjust for daylight savings time.
We're running CF8 on Windows and all dates/times are stored in SQL Server 2005 in Pacific Time. So when Windows auto-adjusts the local clock between Daylight and Standard time, a certain number of our users need to adjust their offset from server time.
I've looked through the International Functions, and none of them seem to convert between timezones. We don't want to turn to a Web Service to get offset/daylight info if it's built into the JVM, and ideally we'd stick with CF functions if they're available. Seems like a common need so I'm surprised there's nothing built into CF to handle this. Is there?
GetTimeZoneInfo() will tell you if daylight saving time is currently in effect on the server, you could use this information along with a checkbox for DST when the user selects their server time offset.