开发者

How do I convert DateTime.Now in the US to local time in the UK?

Hope all of you are enjoying yourself.

My app is running on server in US, and it's using following code to pick the date and time.

var pickUpTime = DateTime.Now.ToShortTimeString();

What I want is that this string should take UK time rather US time.

Can you please share your experience ?开发者_开发问答

Best regards, Abdullah


You can use this code to convert the time:

var now = DateTime.UtcNow;
var timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("GMT Standard Time");
var britishLocalTime = TimeZoneInfo.ConvertTime(now, timeZoneInfo);

This code will account for daylight savings.

If you want to use another timezone you can use TimeZoneInfo.GetSystemTimeZones() to get all timezones and pick the one you need. On my system it returns 91 elements.


you can convert it into GMT and then to whatever you want.

Getting current GMT time


DateTime.UtcNow should do it MSDN

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜