How can I output the current time in California from my website?
I want visitors开发者_Go百科 to know the current time for my application.
TimeZoneInfo pacificZone = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
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.
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.
精彩评论