Reputation: 15455
how do i convert the following datetime string that contains a timezone designation into a datetime type?
here's the string "2011-11-27 23:59:59 EST"
here's what i've found on msdn:
class Program
{
static void Main(string[] args)
{
// I realize here that the types don't match but just trying to
// illustrate what i'm trying to do
DateTime myDate = ReturnTimeOnServer("2011-11-27 23:59:59 EST");
}
public static DateTimeOffset ReturnTimeOnServer(string clientString)
{
string format = @"yyyy-mm-dd h:m:s zzz";
TimeSpan serverOffset = TimeZoneInfo.Local.GetUtcOffset(DateTimeOffset.Now);
try
{
DateTimeOffset clientTime = DateTimeOffset.ParseExact(clientString, format, CultureInfo.InvariantCulture);
DateTimeOffset serverTime = clientTime.ToOffset(serverOffset);
return serverTime;
}
catch (FormatException)
{
return DateTimeOffset.MinValue;
}
}
}
Upvotes: 0
Views: 176
Reputation: 18743
var myDate = DateTime.ParseExact("2011-11-27 23:59:59 EST",
"yyyy-MM-dd HH:mm:ss EST", CultureInfo.InvariantCulture);
Upvotes: 2