In my application I store all transaction dates in UTC in the database.
At the time of displaying the transactions in the UI I convert the date to
local date time and then display the same to the user.
I do the following:
string LocalDtTimeString =
Convert.ToString(DtClass.ConvertToLocalDateTime(System.DateTime.Parse(strDate)));
where strDate is the string representation of the date fetched from the
database.
and the DtClass method implementation is:
public class DtClass
{
:
:
public static DateTime ConvertToLocalDateTime(DateTime UTCDateTime)
{
return UTCDateTime.ToLocalTime();
}
:
:
}
Scenario is:
My UI is in the context of PST. I perform a transaction at 4:30 PM (PST).
Database stores the date as next day morning 12:30 AM (UTC).
Now I changed the Time Zone of my machine from PST (GMT - 8) to
EST (GMT - 5).
I run my UI and see that it shows the transaction time as 10:30 PM (EST)
instead of showing it as 7:30 PM (EST).
This is happening on a test machine.
When I wrote this code and tested it on the development machine, it was
correctly converting times and giving respective local times.
Is there anything that I need to change in the code that will make it work
consistently on all machines?
I looked around regarding this issue on the internet and read some material,
which talks about behavior differences based on the culture notably with
DateTime.Parse and DateTime.ToString.
I don't understand why it subtracts 2 hours instead of 5...
I would really appreciate your help...
Thank you...
-Avinash