Question

I have created a android client server application which involves storing datetime values into database. The server is mssql server. The server side programming is done in c#. The current time is stored in database as datetime2 value. The server time zone is US Mountain Standard Time. And its offset from GMT is -07 hours. That is server time is GMT-7.00. My current local time is GMT+05.30. I have developed this android app to work in GMT+05.30 timezone. So I need to know how to convert the server time from GMT-07.00 to GMT+05.30.

I have founded off the server time and GMT using the following codes.

TimeZone zone = TimeZone.CurrentTimeZone;
DateTime server=DateTime.Now;
DateTime universal=zone.ToUniversalTime(DateTime.Now); //GMT TIME
//I have found that My server offset from GMT is -07.00 using the below code
TimeSpan offset = zone.GetUtcOffset(DateTime.Now); 

When I enter the following values into my server database, it enters into table in this format.

Here my local time is: 2014-02-03 20:25:08.0000000

my server time at that moment is: 2014-03-03 07:55:28.0000000   

the utc time at that moment is: 2014-03-03 14:55:28.0000000 

So in total there is a time difference of about 12 hours and 30 minutes from my server time and local time. If the server at this location goes down by any reason, then the server will run from another location with different time zone and different offset. So the difference between the server time and the local time changes sometimes. This causes a problem to my app, as my values are getting wrong some times. Is there any way to find out the offset when I require and calculate the time of my server and make it same as the local time??

Was it helpful?

Solution

Store your data as a DateTimeOffset if you're using SQL 2008 onwards.

That should make things much easier comparing across different time zones. You can then compare using the ToLocalTime

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top