Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Javascript SQL .NET
Hello Guys,
 
I am stumble in the scenario where i need to convert UTC DateTime stored in the database to local time once the user retrieved the data and rendered in their respective browsers.
 
My current solution is: to get the timezone of the current user and store in session and use that to convert the utc to local time.
 
current problem: the timezone of the current user might vary for daylight saving. and for scenario where the timezone of the current user is in daylight saving mode but the date to convert is in regular time.
 
scenario:
1. user saved data on regular time
2. during daylight saving user open the app and retrieved data
3. when converting the datetime the timezone that will be use is daylight saving
 
Question:
1. What is the best approach to handle datetime conversion from utc to local time where daylight saving is also considered?
2. Is there a way to determine if the utc is in daylight or ordinary time?
Posted 26-Feb-13 20:51pm
Edited 26-Feb-13 22:07pm
v3
Comments
Sergey Alexandrovich Kryukov at 27-Feb-13 2:14am
   
By reading documentation. How can it possibly be a problem? What did you try so far?
—SA
Jephunneh Malazarte at 27-Feb-13 2:21am
   
because the data that was saved was in ordinary time (GMT-5). Now the locale timezone of the user is in day light saving time (GMT-4). so when i use that to convert the time the result will be 1 hr diff.

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  
Comments
Jephunneh Malazarte at 7-Mar-13 21:18pm
   
thanks, i'll accept your solution because this is indeed part of the solution but not the exact one though. The problem is a bit complicated than a mere conversion of UTC to Local DateTime.
Richard MacCutchan at 8-Mar-13 3:49am
   
But it is the exact solution. Whenever you are storing datetime values in a database or other permanent storage, you must always store the UTC value of that time. Then at some later time when you wish to display it, you convert it back to its local time value. In that way it will always be correct and adjusted for daylight savings: that is assuming you use the correct conversion routines.

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

  Print Answers RSS
0 OriginalGriff 490
1 Maciej Los 299
2 BillWoodruff 174
3 /\jmot 170
4 Suraj Sahoo | Coding Passion 150
0 OriginalGriff 8,484
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,639
3 Maciej Los 5,159
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web04 | 2.8.1411023.1 | Last Updated 27 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100