datetime with offset > local and utc

C

chandy

Hi,

I have a datetime+offset as a string and want to get two datetimes
from it; the local and UTC. So far I can only do it by splitting on
the + or - of the offset and creating a new date with and without the
offset. Is there a cleaner way, i.e., using System.DateTime etc. and
not resorting to string splitting? I had read about DateTimeOffset
which seems ideal but this is an ASP.Net 2.0 app and that seems to be
only in Framework 3.5 :(

Chandy
 
M

mpetrotta

Hi,

I have a datetime+offset as a string and want to get two datetimes
from it; the local and UTC. So far I can only do it by splitting on
the + or - of the offset and creating a new date with and without the
offset. Is there a cleaner way, i.e., using System.DateTime etc. and
not resorting to string splitting? I had read about DateTimeOffset
which seems ideal but this is an ASP.Net 2.0 app and that seems to be
only in Framework 3.5 :(

If the format of your date string is predictable, you can do thus:

string s = "2007-02-02 00:00 -8";
DateTime d = DateTime.ParseExact(s, "yyyy-dd-MM hh:mm z",
CultureInfo.CurrentCulture); // 'z' is the time zone offset specifier
Console.WriteLine(d.Kind); // prints "Local"
Console.WriteLine(d); // prints "2/2/2007 12:00:00 AM"
Console.WriteLine(d.ToUniversalTime()); // prints "2/1/2007 8:00:00
AM"

Look up "Custom DateTime Format Strings" in MSDN for more info.

Michael
 
M

mpetrotta

Console.WriteLine(d.Kind); // prints "Local"
Console.WriteLine(d); // prints "2/2/2007 12:00:00 AM"
Console.WriteLine(d.ToUniversalTime()); // prints "2/1/2007 8:00:00
AM"

Minor error: the output is actually:

Local
2/2/2007 12:00:00 AM
2/2/2007 8:00:00 AM

....as it should be.
 
M

mpetrotta

Minor error: the output is actually:

Local
2/2/2007 12:00:00 AM
2/2/2007 8:00:00 AM

...as it should be.

Ach. Too many replies to myself.

As I try it again, I see that a simple parse works fine:

string s = "2007-02-02 00:00 -8";
DateTime d = DateTime.Parse(s);
Console.WriteLine(d.Kind); // prints "Local"
Console.WriteLine(d); // prints "2/2/2007 12:00:00 AM"
Console.WriteLine(d.ToUniversalTime()); // prints "2/2/2007 8:00:00
AM"

Michael
 
P

Peter Duniho

Hi,

I have a datetime+offset as a string and want to get two datetimes
from it; the local and UTC. So far I can only do it by splitting on
the + or - of the offset and creating a new date with and without the
offset. Is there a cleaner way, i.e., using System.DateTime etc. and
not resorting to string splitting? I had read about DateTimeOffset
which seems ideal but this is an ASP.Net 2.0 app and that seems to be
only in Framework 3.5 :(

How are you converting the strings now?

The Parse() and ParseExact() methods for DateTime include an overload
that takes a DateTimeStyles parameter. Using that parameter you can
specify the AdjustToUniversal style (flag), which will cause the
DateTime value formatted as a local time with offset to stored as a
universal time.

So, you can parse the string twice, once with the flag and once without
to get two different versions of the DateTime.

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top