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.