Avatar of schwarz2
schwarz2 asked on

Working with DateTime independent of Timezone

Hello,

I am working with a VB.NET 2005 application in which I need to store date and time information.  I'm using a datagridview which is backed by a datatable (unbound to a database).  At the end of every session, I write this datatable to an XML file, and in new sessions I attempt to reload the datatable from the stored XML (ReadXML/WriteXML methods).

My goal is to make the application independent of timezones.  If I'm in GMT-5 and store datetime info, I want to be able to write this to the XML file, then change my computer to GMT-8, reload the datatable, and see that datetime exactly as originally input.  5:00pm is 5:00pm whether in Eastern Time or Pacific Time.

Of course, the answer typically suggested is to use Universal Time.  That's okay, but I need help in making the application stay in the Universal Time mentality.  I need the datagridview to not do any conversions to local time and to properly write to the XML with the +00:00 extension.

Any ideas?
(By the way, if you can answer this question, you can probably answer this person's question which is about to be deleted, I think:  https://www.experts-exchange.com/Programming/Languages/.NET/Class_Libraries/ADO.NET/Q_23307356.html ).

Many many thanks.
.NET ProgrammingProgrammingVisual Basic.NET

Avatar of undefined
Last Comment
schwarz2

8/22/2022 - Mon
ororiole

If you are using the date and time as a timestamp, and do not actually want to operate on it as a Datetime object, why not just make the date and time a string? That way, you store it as a string, retrieve it as a string, and its already a string when you display it. Your local app wont try and convert it because, its a string.
And if you did want to update the hour for example, you string search for first index of ':', replace the 2 chars preceeding it, and go on your way.
ASKER
schwarz2

I actually am going that route currently.  I am trying to convert it to a more efficient usage of datetime since using a string effectively breaks the sorting mechanism (5/1/2008 comes before 7/1/2007 since alphabetic sorting takes over).

There are ways around that, but it just makes things overly hairy.  If it is possible to use the datetime type, I would much rather go that route.  As mentioned, timezones are the only caveat.  Frustratingly so.

(theoretically, I could use a hidden datacolumn that mirrors the string datacolumn and override the sorting routine.  But is that really necessary?)
Gary Davis

The DateTime value is aready independent of the timezone. It is stored base on GMT. At display time, the datetime is converted to a printable format based on the timezone if desired.

Gary Davis
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
ASKER
schwarz2

Thanks Gary, I understand this.  My question is really:
1) How can I force my dates to be stored in GMT time?  If I do datetime.parse, it assumes it's local time.  If I use a datetimepicker again it is converted to local time.  When performing the datatable.writexml routine, the values are written as, for example, 2008-06-02T17:00:00-04:00.

2) How can I get the datagridview to display the GMT time?  The datagridview automatically converts the values to local time.
ASKER CERTIFIED SOLUTION
ororiole

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
schwarz2

Much thanks ororiole.  You know what it ended up being:  the datacolumn datetimemode had to be specified as DataSetDateTime.Utc.  If I simply set the datetime value to utc, it had no effect once it was placed into the datacolumn.

You set me on the correct path.  Thanks.