Be carefull with DateTime.ToUniversalTime()

Note: this entry has moved.

It's common practice to use that method when saving dates to a DB or processing the dates. The reason is that you should always account for users being in different time zones. But the DateTime structure has nothing inside it that indicates if it's a UTC date or a local one, therefore, each time you call that method, the current time zone offset is calculated and added/removed from the DateTime instance. If you call it multiple times, you will end with the wrong DateTime.
Wouldn't it be cool if DateTime knew if it has already been converted to UTC?

No Comments