2

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

Martin Liversage
  • 104,481
  • 22
  • 209
  • 256
Abdullah Saqib
  • 161
  • 1
  • 2
  • 6
  • Even if you adjust the timezone it would still be based on your system's clock which doesn't sound like what you want to do. Post your code. – Security Hound May 27 '11 at 11:23
  • I assume that what you want is the current local time in the UK. However, during winter this time happens to be the same as UTC and you are going to get answers that tell you how to convert to UTC. Had you asked how to get the local time in say Beijing that would not have happened. – Martin Liversage May 27 '11 at 12:10

3 Answers3

7

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.

Martin Liversage
  • 104,481
  • 22
  • 209
  • 256
  • +1 Agreed. Use `UtcNow` to get the base time and then offset it from there. @AbdullahSaqib you should start accepting answers. – Chase Florell Feb 14 '12 at 01:58
1

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

Getting current GMT time

Community
  • 1
  • 1
Illuminati
  • 4,539
  • 2
  • 35
  • 55
  • The question you link to is really how to __not__ use `DateTime.Now`. However, the highest rated answer is basically to use `DateTime.UtcNow` which does not account for daylight savings. – Martin Liversage May 27 '11 at 12:07
  • @Martin, agreed. I just wanted to shed some light on guy ;) - But your answer is more on the point. thanks. – Illuminati May 27 '11 at 12:19
0

DateTime.UtcNow should do it MSDN

Tom B
  • 2,160
  • 2
  • 13
  • 15
  • 1
    -1: It won't. Current UK time is British Summer Time (BST) which is one hour ahead of UTC/GMT. – Richard May 27 '11 at 11:36