Practice safe DateTime manipulation

comments edit

What is the proper way to add three hours to a DateTime for the PST timezone? Is it this?

DateTime d = DateTime.Parse("Oct 26, 2003 12:00:00 AM");d = d.AddHours(3.0);

A valiant attempt, but wrong. Instead try this:

DateTime d = DateTime.Parse("Oct 26, 2003 12:00:00 AM");
d = d.ToUniversalTime().AddHours(3.0).ToLocalTime();
// displays 10/26/2003 02:00:00 AM which is correct!

Why all the rigamarole of converting to universal time and back to local time? A little thing we like to call Daylight savings time. Without the conversion, adding three hours would have set the time to 3:00 AM which would be wrong.

Realizing this probably would have saved me from many hours of intense debugging sessions. I remember one particular case with a system of scripts and tools I set up to analyze the very large log files for a big client. It hummed along nicely until one fine spring day when it failed miserably. After an entire day of tracking down the source of the problem, I finally nailed it down to a script’s mishandling of Daylight Savings.

To find out more about proper DateTime handling, read the following article concerning best practices with manipulating date times.