tags:

views:

66

answers:

2

How can I output the current time in California from my website?

I want visitors to know the current time for my application.

+1  A: 

Is your server located in California? If so, then DateTime.Now will return the current time in the current timezone.

If not, you can either hard-code the offset from UTC of California and use DateTime.UtcNow.AddHours(offset) or you can use a database such as the tz database which will help with things like automatically adjusting for daylight savings and so on.

There is also the TimeZoneInfo class, introduced in .NET 3.5 that lets you enumerate the timezone information installed into windows. You should be able to calculate it from that as well.

Dean Harding
+2  A: 
TimeZoneInfo pacificZone = TimeZoneInfo.FindSystemTimeZoneById("US/Pacific");
DateTime pacificTime = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, pacificZone);

The advantage of this is that it doesn't depend on what time zone the server is currently set to.

Matthew Flaschen
Isn't there a way that returns a DateTimeOffset? That would be nicer IMO. My solution (deleted) doesn't work :-(
dtb
@dtb, why do we need to explicitly get the offset? He said he wants to output the actual time.
Matthew Flaschen
+1. Nice. TimeZoneInfo is new in .NET 3.5.
Steven