Translating to local time

  • Thread starter Thread starter Marco Shaw
  • Start date Start date
M

Marco Shaw

Some examples in datetime format:
Thu, 25 Jan 2007 10:22:39 -0500
Thu, 25 Jan 2007 09:12:17 -0800
Thu, 25 Jan 2007 10:26:04 -0800
Thu, 25 Jan 2007 10:27:26 -0800
Thu, 25 Jan 2007 19:28:28 +0000

What's the easiest way to get these into my local timezone (AST-Atlantic
Standard Time)?

Change them to GMT, then do -0400?
 
Kevin Spencer said:
The first thing you need to do is to determine what Time Zone they
originated from.

That's what the -0500, -0800, -0800 etc are for. All relative to UTC.
 
Marco Shaw said:
Some examples in datetime format:
Thu, 25 Jan 2007 10:22:39 -0500
Thu, 25 Jan 2007 09:12:17 -0800
Thu, 25 Jan 2007 10:26:04 -0800
Thu, 25 Jan 2007 10:27:26 -0800
Thu, 25 Jan 2007 19:28:28 +0000

What's the easiest way to get these into my local timezone (AST-Atlantic
Standard Time)?

Change them to GMT, then do -0400?

Try this: (works on my box, but I'm in GMT!)

using System;
using System.Globalization;

class Program
{
static void Main(string[] args)
{
string[] data = new string[]
{"Thu, 25 Jan 2007 10:22:39 -0500",
"Thu, 25 Jan 2007 09:12:17 -0800",
"Thu, 25 Jan 2007 10:26:04 -0800",
"Thu, 25 Jan 2007 10:27:26 -0800",
"Thu, 25 Jan 2007 19:28:28 +0000"};

foreach (string x in data)
{
Parse(x);
}
}

static void Parse (string x)
{
DateTime dt = DateTime.ParseExact
(x, "ddd, dd MMM yyyy HH:mm:ss zzz",
CultureInfo.InvariantCulture,
DateTimeStyles.AdjustToUniversal);

dt = dt.ToLocalTime();
Console.WriteLine (dt);
}
}
 
I thought so, but I was unfamiliar with the DateTime format syntax being
used, so I was not going to assume that this was the case. After doing some
research, I have found this format as part of RFC 822 (ARPA Internet Text
Message).

--
HTH,

Kevin Spencer
Microsoft MVP
Software Composer
http://unclechutney.blogspot.com

The shortest distance between 2 points is a curve.
 
Back
Top