views:

82

answers:

6

Hello All,

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

+2  A: 

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
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
@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
@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
A: 

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

Secret Agent Man
Convert to local time would not do because that would be local to the US.
0xA3
What I mean is, convert to the desired local time. In this case, Egyptian time.
Secret Agent Man
+2  A: 

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: http://stackoverflow.com/questions/246498/creating-a-datetime-in-a-specific-time-zone-in-c-fx-3-5

James Black
A: 

Server time will never be the same as client time.

You can get the client time using javascript:

var currentDate = new Date();
Oded
+1  A: 
DateTime now = TimeZoneInfo.ConvertTime(DateTime.UtcNow, 
    TimeZoneInfo.FindSystemTimeZoneById("Egypt Standard Time"));
0xA3
A: 

Different regions of the world move the clocks forward or backward at different times. So even if you program a time zone adjustment, you may still have problems.

The idea of getting the client time with javascript seems to offer a solution.

My question is, how do you pass the information obtained with javascript to the server, using ASP.NET?

Thank you in advance.

Eric