2

I have a great problem and I don't know how to solve it

I have created a web application and host it in USA server , and the users who access this web application from Egypt

The problem that i'm using DateTime.Now method inside the application and this method return the time of USA server not the time of the machine that use it in Egypt

How can i solve this problem ? please help me as soon as you can

Thanks in Advance

Amira Elsayed Ismail
  • 9,216
  • 30
  • 92
  • 175

4 Answers4

5

Well, you could start off by using DateTime.UtcNow, transferring that to the client, and then performing the relevant time zone conversions at the client. That's usually a reasonable way of working - so long as you can do the processing at the client (e.g. in JavaScript).

Alternatively, ask the user their time zone so you can serve pages which use the local time, using TimeZoneInfo. Of course if you know all your users are in Egypt you could potentially just hard-code that.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Thanks Mr.Jon , did you mean that I have to use DateTime.UtcNow method in my application then , I can define a variable with the time zone of Egypt , for example = 2 then I add this value to DateTime.UtcNow every time I insert data to database – Amira Elsayed Ismail Oct 04 '10 at 11:01
  • 1
    @Amira Elsayed - Unless your users will always be in the same timezone you may want to store the time as universal time in the database. – James Black Oct 04 '10 at 11:11
  • 1
    @Amira: As James says, it's *usually* a good idea to keep the database in UTC. Perform conversions when you *display* the data. – Jon Skeet Oct 04 '10 at 11:13
  • i have saved the date as `DateTime.UtcNow` and converted to local time to `Arab Standard Time` as @Dirk Vollmar answered, but does `stackoveflow` also stores all their time in `UTCNow` because all users are from different time zone ? – Shaiju T Dec 02 '15 at 11:33
  • 1
    @stom: I'm not sure that this is really relevant to the question, but yes, I believe Stack Overflow does everything in terms of UTC. – Jon Skeet Dec 02 '15 at 11:35
4

You need to convert the datetime value to the time zone of your users. One simple way to do this would be to call ConvertTime and pass in the desired time zone as follows:

DateTime now = TimeZoneInfo.ConvertTime(DateTime.UtcNow, 
    TimeZoneInfo.FindSystemTimeZoneById("Egypt Standard Time"));
Dirk Vollmar
  • 172,527
  • 53
  • 255
  • 316
  • [here](https://msdn.microsoft.com/en-us/library/ms912391%28v=winembedded.11%29.aspx) is `Time Zone Index Values` from `msdn`, may help someone. – Shaiju T Dec 02 '15 at 11:26
2

Generate the time using UTS, so that it is normed, and then display based on the location of the user or on their preferences.

You may want to read this also: Creating a DateTime in a specific Time Zone in c# fx 3.5

Community
  • 1
  • 1
James Black
  • 41,583
  • 10
  • 86
  • 166
0

Well, you could use DateTime.UTCNow and then convert to local time.

Doctor Blue
  • 3,769
  • 6
  • 40
  • 63