Parse date and time and convert to UTC.
using System;
using System.Globalization;
public class Test
{
public static void Main()
{
string dateString, format;
DateTimeOffset result;
CultureInfo provider = CultureInfo.InvariantCulture;
dateString = " 06/15/2010 15:15:30 -05:00";
format = "MM/dd/yyyy H:mm:ss zzz";
try
{
result = DateTimeOffset.ParseExact(dateString, format, provider,
DateTimeStyles.AllowWhiteSpaces |
DateTimeStyles.AdjustToUniversal);
Console.WriteLine("'{0}' converts to {1}.", dateString, result.ToString());
}
catch (FormatException)
{
Console.WriteLine("'{0}' is not in the correct format.", dateString);
}
}
}
Related examples in the same category