I host my website (asp.net webforms, .Net 4.5, SQL Server 2012) on Godaddy and they (the server) use Mountain Standard Time
(-7 compare to UTC
time) which is never changed and does not observe daylight saving time.
So if I do
Response.Write("UTC Time: " + DateTime.UtcNow.ToString() + "<br/>");
Response.Write("Server Time: " + DateTime.Now.ToString() + "<br/>");
Response.Write("Server DateTimeOffset: " + DateTimeOffset.Now.ToString() + "<br/>");
It will display this:
UTC Time: 9/18/2015 5:14:09 PM
Server Time: 9/18/2015 10:14:09 AM
Server DateTimeOffset: 9/18/2015 10:14:09 AM -07:00
But my users are located in Atlanta, GA which does observe daylight saving time and according to timeanddate.com they use EDT in the summer and EST in the winter
How do I get correct current time of user? Let say user open my web-application and hit Show my time
button, it will display a correct current user's time?
Take a look at this example. This is quite easy.
You can use TimeZoneInfo.ConvertTime to convert from one time zone to another. However, if you just convert to EST or EDT, it will always display that value. If you want to always display the correct time for the client, you'll need to do something like using Javascript to store the client browser's local time zone in a cookie, then use that value as the time zone to convert to.
It might be a little more streamlined to get UtcTime from the server and convert from that using TimeZoneInfo.ConvertTimeFromUtc, if you expect to do much of this. Basically the same process, though.
You should never rely on the time zone settings of a server. Therefore
DateTime.Now
should not be ever used in an ASP.NET application.There are many other reasons to avoid
DateTime.Now
. Read:The case against DateTime.Now.The local time of the server is never guaranteed to be the local time of the user of your website anyway. If it is, it's just coincidental.
The easiest way to get the user's current time in their time zone is via JavaScript:
Though this is based on the user's clock, which may or may not be correctly set. To have any guarantees about the time, you'd have to use the UTC time of the server's clock, with the user's time zone applied.
One approach you might consider is to send the UTC time of the server down to the client, then load that into JavaScript in the client to project it to their time zone:
Actually detecting the user's time zone is a hard problem that does not currently have a solution. Some may recommend
new Date().getTimezoneOffset()
, but that only gives you the current numeric offset, not the time zone. Offsets can change for DST, and many time zones use similar offsets. There are also complications for historical dates near DST transitions that will work against you.Scripts like jsTimeZoneDetect can guess your IANA time zone ID, such as
America/New_York
for Eastern time, but they are not 100% accurate. If you need the user's time zone on your server, then ultimately should ask the user for their time zone somewhere in your application.In .NET, you can use Noda Time to work with IANA time zones. Without Noda Time, .NET has the
TimeZoneInfo
class, but it can only work with Windows time zones.If you know for certain that the users are in Atlanta, GA (which is in the US Eastern time zone), then you can do this:
Or, with Noda Time and IANA time zone IDs: