barak ben horin
barak ben horin

Reputation: 534

datetime.now is 10 hours late only on the server, on the local machine sets right

i have a web application where the datetime.now is very important. on my local machine it's sets datetime.now to the right time. only when i use it on the server ( my server sits in europe, i'm in israel ) there is a 10 hours diffrence between the real time and the datetime.now i've tried allready :

<globalization culture="he-IL" uiCulture="he-IL" />

also i've tried to set each page / set the datetime variables direct to local time :

DateTime example = DateTime.Now.ToLocal();

and still , nothing seems to work. any suggestions? thanks in advance.

Upvotes: 2

Views: 1711

Answers (3)

giammin
giammin

Reputation: 18958

Setting the application Culture changes how number, datetime etc etc are displayed

Does not affect time zone because a culture could be valid for different timezone.

You should handle datime as utc

http://msdn.microsoft.com/en-us/library/system.timezone.aspx

var timeZone = TimeZoneInfo.FindSystemTimeZoneById("YOUR TIME ZONE ID");
var dateTime = TimeZoneInfo.ConvertTime(DateTime.UtcNow, timezone)

Upvotes: 3

Xaisoft
Xaisoft

Reputation: 46611

If the server is 10 hours behind, it is going to return you its local time, not yours. What if you add 10 hours to the server's local time to get your local time in Israel. You might have to give or take and hour if you have to observe daylight savings time.

var dt = DateTime.Now.AddHours(10);

Upvotes: 2

Marc Gravell
Marc Gravell

Reputation: 1063058

DateTime.Now is local, so converting to local is a no-op; if it isn't right, try DateTime.UtcNow, or combinations involving starting from UtcNow and applying ToLocal from there.

Also ensure the server's clock and timezone are correct.

To be honest, using UtcNow throughout may be easier.

Upvotes: 7

Related Questions