January 14, 2010
@ 03:38 PM
Today at work we ran into an issue where we needed to convert nullable DateTime values to UTC time values. Of course this can't be done if there isn't a value present. Most programmers first thought is to wrap this in an "if" statement. There is a much easier way. Here is a code example:

   DateTime? testDate = DateTime.Now;

 

   DateTime? newDate = testDate.HasValue ? TimeZoneInfo.ConvertTimeToUtc(testDate.Value) : testDate;


With this code, if there is a local time value, it will be converted with no Exceptions being thrown.




 
All comments require the approval of the site owner before being displayed.
Name
E-mail
Home page

Comment (HTML not allowed)  

Enter the code shown (prevents robots):

Live Comment Preview