On Aug 1, 7:40 am, cha...@totalise.co.uk wrote:
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