0

hello im mohammed from sri lanka and new to programing so i am litle bit confused these days

So we have a server in srilanka and we have clients are access to the website from several countries and make reservations then the server saves the time acording to srilankan time zone so i wont store actual time i need hep to resolve thease problems Helpme!and serve me!

EG:

client make order at 3.10am from USA it saves 3.10am and make Oder srilankan time 3.10 am so i want to atomatically conver to clint country time

how can i code that using C# visual studio 2010

thanks

Tigran
  • 61,654
  • 8
  • 86
  • 123
Moahmmed
  • 11
  • 3
  • The following question provides some useful information on how to handle timezones: http://stackoverflow.com/questions/2532729/daylight-saving-time-and-timezone-best-practices – Jakob Christensen May 25 '12 at 09:15

1 Answers1

3

On the client side convert the time to GMT time using DateTime.ToUniversalTime and store it in your database. Then read it converting as local time zone.

E.g. in your code behind do something on the following lines. Code from MSDN.

System.DateTime univDateTime = localDateTime.ToUniversalTime();

to convert it back

System.DateTime localDateTime;
try {
    localDateTime = System.DateTime.Parse(strDateTime);
}
catch (System.FormatException) {
    System.Console.WriteLine("Invalid format.");
    return;
}
ABH
  • 3,391
  • 23
  • 26
  • this steps helps me and can you explain it more i m really new – Moahmmed May 25 '12 at 10:01
  • @Moahmmed it's better that in the server db you save datetime as universal time (utc or gmt). on the client side whenever you get the time you should convert it to universal time using DateTime.ToUniversalTime and store it in local db. Whenever you want to convert it to local time in any time zone you can do this by using TimeZone.CurrentTimeZone.ToLocalTime(datetime) – ABH May 25 '12 at 10:12