ASP.NET: Time keeps on slipping, slipping...

There are things that sound hard, such as build a table with edit, update, delete, and sorting, but in ASP.NET are trivial. Then there are things that sound trivial, like managing time zones, that are a real PITA.

.NET DateTime objects do not store any type of flag to say "I'm an EST object" - they just assume things. If you say myDateTime.ToUniversalTime() .NET assumes myDateTime is a local time object. If you say myDateTime.ToLocalTime() .NET assumes you have a UTC time. So *you* better know what you put in myDateTime because .NET won't remember.

Okay, this isn't a show stopper, just unexpected. We can make life simple by storing and using only UTC times, then convert to local time on display only. This means using DateTime.UtcNow instead of DateTime.Now, which is local. Once you have your site in UTC time, an idea might pop in your head, "Hey, it should be no problem to add a member to Profile to let users choose the time zone they want to see posts in!" Pop that idea right out...

.NET contains no methods to convert a time to an arbitrary time zone. Once you consider Daylight Savings time issues, you really, really start to wonder how such a feature could slip. It was planned for 1.0, planned for 1.1, planned for 2.0, and is planned for the next update. I decided at this point I would convert to EST for my postings, and deal with Profile members another day. I still need to take into account Daylight Savings, but I'm lucky that my (PST) web server also observes US Daylight Savings at the same time I do here in good ole EST. Thus, the following will convert a UTC date to EST on a PST server (I'm willing to be portable only to a certian extent):
int offset = TimeZone.CurrentTimeZone.GetUtcOffset(date).Hours + 3;
return date.AddHours(offset).ToString(format);
Adapting this to your server, or allowing users to select their time zone, is left as an exercise for the reader.

Posted By Mike On Saturday, May 20, 2006
Filed under asp.net datetime | Comments (1)

Submit this story to DotNetKicks   

Gabriel - Thursday, May 25, 2006 2:18:28 PM

life is a bitch

Leave a comment



Your name:
 

Your email (not shown):
 
Will display your Gravatar image.

Your website (optional):



About Me

Michael C. Neel, born 1976 in Houston, TX and now live in Knoxvile, TN. Software developer, currently .Net focused. Board member and President of ETNUG, and organizes CodeStock, East Tennessee's annual developers conference. .Net speaker, a Microsoft ASP.NET MVP and ASPInsider. Founder of FuncWorks, LLC and Feel The Func podcast.

Proud father of two amazing girls, Rachel and Hannah.

 Subscribe to ViNull.com |  Comments

Follow me on Twitter | Contact Me

Related Posts

ASP.NET: Cannot use a leading .. to exit above the top directory

This is a really quick post, mostly for myself so the next time I have this issue I can find the answer (yes, I often search my blog before google). The ... Read more

ASP.NET: Creating a UserControl with Child Content

I love ASP.NET User Controls, aka “ascx” files.  These little guys are great for reusable content and dividing up the components of a website.  ... Read more

ASP.NET SEO Interview on Polymorphic Podcast

Craig Shoemaker just posted the latest episode of the Polymorphic Podcast: ASP.NET SEO - Interview with Michael Neel.  Yes, I've now appeared in a ... Read more

Google Can You Hear Me?

In June of 2006 I wrote an article titled "Google Can You Hear Me? How to design URLs that are search engine friendly" that also included a sidebar titled ... Read more

Using LINQ to generate HTML

I hate seeing code mixed with markup. Seeing a template page with <% if(show) { %> makes me want to claw my eyes out.  Seeing String htmlTitle ... Read more

FeelTheFunc Podcast

CodeStock
Are you going?

ASPInsiders Member

ETNUG Member