开发者

How do I output time for california?

开发者 https://www.devze.com 2022-12-30 04:44 出处:网络
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.FindSystemTimeZoneByI

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消