Click here to Skip to main content
15,885,847 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Suppose we have to save Norwegian time 9:00
If we are ignoring day light.In database in UTC we will save 8:00. In Display we will add +1 for Norway 9:00. In India we are adding +5:30 means 1:30 – it should be 12:30, 1 hour difference

If we consider day light then we should add user +2 for Norway Means in database we will save 7:00 And for Norway for +2 it will be 9:00 And for India it will be 12:30 – correct time. But in this case if we want to see old dates which we have added when day light was false with +1 But now if we show old dates by adding +2 hours. Then it will be 1 hour earlier.

MY Problem

What i am doing is getting local time entered by user.

and converting it to UTC by adding offset hour for user timezone.

And while getting i am passing offset hour for user timezone to SQL and in SQL i am converting UTC date to user local time.

and here the issue comes old dates which i have entered with +1 before 31 march.After 31 march as user offset will be +2 because of day light.So old dates shifted by one hour.


Please correct me where i am doing wrong in handling dates.
And how can i solve my problem
Posted
Updated 13-Jun-14 17:40pm
v4

1 solution

I would save as UniversalTime in the database
C#
var toSaveInDb=    TimeZone.CurrentTimeZone.ToUniversalTime(DateTime.Now)

then any country you can get it back as below
C#
var fromDBtoUI  =  TimeZone.CurrentTimeZone.ToLocalTime(myUtcDateTime)


references
http://msdn.microsoft.com/en-us/library/system.datetime.touniversaltime.aspx[^]
How to get UTC equivalent for my local time in C#[^]
You need to detect client culture and set it as current thread culture.
http://weblog.west-wind.com/posts/2014/Mar/27/Auto-Selecting-Cultures-for-Localization-in-ASPNET[^]
 
Share this answer
 
v3
Comments
Anuj Banka 13-Jun-14 8:11am    
but it will give timezone for server not for client.
DamithSL 13-Jun-14 23:52pm    
So you know that you need to detect client culture and use that for culture specific activities like above.
I have added one link for that. Your can find many resources on this topic.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900