I want to convert my time to universal time when I store date time in database and convert back to system time when I retrieve from the database.
Both of them I have done it success fully by creating functions ConvertToLocalTime()
and ConvertToUniversalTime().
The code for convert to local time is given below:
public void ConvertToLocalTime()
{
string szTimezoneinfoid = TimeZoneInfo.Local.Id.ToString();
TimeZoneInfo tzInfo = TimeZoneInfo.FindSystemTimeZoneById(szTimezoneinfoid);
this.m_dtCreatedDate = TimeZoneInfo.ConvertTimeFromUtc(m_dtCreatedDate, tzInfo);
}
When I call a function Shipment.ConvertToLocalTime() it changes the shipment created date to local time till this it works fine.
Now if I call the Shipment.ConvertToLocalTime() again then it consider the local time again as UTC and convert it to local time.
Is there any way to block the conversion to local time if it is already in local time?
Does there any way that we can get the time zone of the datetime?
I’m not sure exactly what’s going wrong in your example, but if you simply want to get a date/time you know to be UTC from the database and convert it to local time, you can do the following:
In my own project, I check
dbTime.Kind != DateTimeKind.Localfirst, jut in case my data reader has decided to start performing the conversion for me.