Get current date time from server and convert it into local time in c#
Asked Answered
C

6

14

Help: I have a server which is having time in GMT-07.00 hours. My local time is GMT+05.30 hours. I need to get current date and time from server and convert this date and time into my local time. I have tried many codes, but still have not found a successive way of doing this. Can somebody please help me out.

string zoneId = "India Standard Time";
TimeZoneInfo tzi = TimeZoneInfo.FindSystemTimeZoneById(zoneId);
DateTime result = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, tzi);
DateTime cu = result.ToUniversalTime();
DateTime cur = cu.ToLocalTime();

I have tried all the above methods, still I'm not getting the correct answer. Suppose my current local time is 09-Mar-2014 12:51:00 PM, then my server time would be 12.30 hours different from my local time, ie subtract 12 hours and 30 minutes from my local time to get my server time. I need to get my local time from the server time. How can it be acheived?? Please suggest me some solutions.. Thanks in advance for your answers

Caliper answered 9/3, 2014 at 7:26 Comment(4)
don't you get your server time equvalent of local time if you subtract 12:30 from your localtime?Wraf
I want to get my local time using my server time. So when I try to add 12.30 to my server time using datetime dt=datetime.now.addhours(12:30); but still not working..Caliper
did any of answers solved your problem?Patino
I'm currently checking it out.. I will tell you the result afterwards.. Thanks...Caliper
P
24

no need to know server time zone. if the server time setting is correct you can try this :

DateTime serverTime = DateTime.Now; // gives you current Time in server timeZone
DateTime utcTime = serverTime.ToUniversalTime(); // convert it to Utc using timezone setting of server computer

TimeZoneInfo tzi = TimeZoneInfo.FindSystemTimeZoneById("India Standard Time");
DateTime localTime = TimeZoneInfo.ConvertTimeFromUtc(utcTime, tzi); // convert from utc to local

to check it locally , change your computer timezone to match your server. then run the code. I check and it's working fine.

update:

the first two lines can be mixed into one line as below . that has a better performance :

DateTime utcTime = DateTime.UtcNow;
Patino answered 9/3, 2014 at 9:42 Comment(3)
The first two lines can be reduced to DateTime.UtcNow, which performs much better.Transact
@MattJohnson : you're right. I just wanted to show the steps more clear.Patino
Sure, but if you peek into DateTime.Now, you'll see that by calling DateTime.Now.ToUniversalTime() you'd actually be doing TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, TimeZoneInfo.Local).ToUniveralTime(). So it's not just about clarity, it's actually slower and more expensive. In other words, DateTime.UtcNow is actually the direct call to the Windows system clock.Transact
W
1

if you want to add 12 Hours and 30 minutes to your Server time to get equavalent localtime(assuming you have server time), you can use AddHours() and AddMinutes() functions to add the 12:30 hours

Try This:

DateTime dt= /*your server time*/;
dt=dt.AddHours(12);
dt=dt.AddMinutes(30);
Wraf answered 9/3, 2014 at 7:36 Comment(4)
Also the server time difference changes when the server at location becomes down, then the server loads from other area with different time zone. So the time difference is always varying..Caliper
are you able to identify the Timezone info from where the Server time is coming ?Wraf
The time zone is Us mountain time zone. with a difference of 07 hours from GMT. that is GMT-07:00 hours.Caliper
Your answer will not work, since I need current date, taking datetime.now and adding 12 hours and 30 minutes won't give the result for my problem.Caliper
I
1

So you have the server's time, and you know the server's time zone? Then you can get the local time like this:

//Server: 09-Mar-2014 11:00:00 AM:
var serverTime = new DateTime(2014, 3, 9, 11, 00, 00);

var serverZone = TimeZoneInfo.FindSystemTimeZoneById("US Mountain Standard Time");
var localZone = TimeZoneInfo.FindSystemTimeZoneById("India Standard Time");

var localTime = TimeZoneInfo.ConvertTime(serverTime, serverZone, localZone);
// => 09-Mar-2014 11:30:00 PM
Impress answered 9/3, 2014 at 9:21 Comment(0)
T
1

If your server's clock is set correctly (regardless of time zone), then the first three lines of your own code are exactly correct. The result variable contains the local time in the India time zone.

Simply omit the last two lines.

Transact answered 10/3, 2014 at 15:57 Comment(0)
E
0
 DateTime serverTime = DateTime.Now;

 DateTime utcTime = serverTime.ToUniversalTime(); 

// convert it to Utc using timezone setting of server computer

 TimeZoneInfo tzi = TimeZoneInfo.FindSystemTimeZoneById("India Standard Time");

 DateTime localTime = TimeZoneInfo.ConvertTimeFromUtc(utcTime, tzi); 

// convert from utc to local
Endue answered 4/8, 2014 at 8:21 Comment(0)
W
0

The simplest way to get the UTC date and time of the server is (SELECT GETUTCDATE();). Try it.

Waterside answered 6/11, 2018 at 5:41 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.