Wherever you have dates displayed on your page, just pass them through this function first in order to convert them to the snippet of JavaScript that does the conversion.Date & Time in Python (Different Date Formats) (Video 54) The conversion is handled directly in JavaScript give the appropriate UTC date.
Wherever in your code, you display the date, you will want to replace it with this script. Tz = TimeZoneInfo.GetSystemTimeZones() // returns all timezones on the current local systemįor (int i = 0 i document.write(new Date(' UTC').toLocaleString()) ", strDate) Or you can iterate through the Timezone's using: Then you can easily work with all the data you have by offsetting the stored UTC dates on the server itself. Maybe have a timezone database table, with the offset values. Along with the user's account, you'd save their timezone. A few approaches on this one are as follows: We can't do this from the server, as it doesn't have that information available to it. Client side we are still going to need the user's timezone offset in order to be able to work with it. Which takes care of our needs, server side. Lucky for us SQL Server has the function "GetUTCDate()" that returns the current UTC time and the DateTime object in C# does the same. Any of these functions will return you the UTC timeĭateTime time = () // sets a DateTime object to the current UTC timeĭateTime time2 = DateTime.UtcNow // same as aboveĭateTime time3 = TimeZoneInfo.ConvertTimeToUtc(DateTime.Now) // problem with this one is that it will use the //local timezone of the server to calculate utc NET have functions for just this type of situation: By knowing the UTC time, we can always work our way to the correctly formatted time for anyone in the world. The answer is to always save the date in UTC format and worry about the display logic afterward. So how do we display to everyone the correct data based on their locations in the world? And if we store it in UTC time, we'd both see that it was published at 4 am, since our offset is -8 hours. Stored using your run of the mill GetDate() function, my reader and I would see that my post was published at 11 pm (New York Time) even though it is 8 pm for me and 9 am for them. My shared hosting server is in New York and I have a reader in Wyoming. :/) I write posts, and each post is given a publish date. And let's assume for the scenario we don't have access to the registry.
Unless you're on Ubuntu, you can't set the server time to be represented in UTC, without some nifty tweaks to the Windows Registry. So time isn't as simple as saving "my" current time and showing it to someone in the world, unfortunately. Just remember that "everything that happens now, is happening now".
For the sake of brevity and my sanity, I will just cover the basics on how to handle UTC. Most of Arizona doesn't for example, except for the Navajo Nation.
You might also need to account for Daily Savings Time, if applicable. For example, I live in California, which uses the Pacific Timezone which is -8 hours offset from UTC. Parts of the world are split into their own time zones, for business, commercial, and legal reasons and each time zone is an offset of the UTC time. UTC (or Coordinated Universal Time for long) is the time standard that the whole world uses to regulate its clocks. One minute it could be in New York, and the next somewhere overseas. Personally, I'm not concerned where my server is, just that it stays up and running. Everything was based on the local time relative to the server's timezone. When looking for solutions to this issue, I mainly got server side answers.
(he's so negative ) Other times, they could be more important values, like a sale order time or activity tracking on your website. So what if they're off by a few hours to some users. For example, news publish times, or comment dates. The same goes for SQL Server's GetDate() function. Sometimes, however, that isn't enough.ĭateTime.Now() will return the current time that the server is currently set to. That's how you would normally work with dates anyhow. When the record is created this column will have a default value of the current date and time on the server. In the Column Settings tab you would set the default value to the GetDate() pre-defined function in SQL Server and we are done.