Monday, July 23, 2012

Atom RSS date,ISO8601 to SQL date conversion C#

On a daily basis we deal with date of the following format 
yyyy-MM-ddThh:mm:ssZ known as ISO 8601, the International Standard for the representation of dates and times, and we have to convert it other formats,mainly SQL.

More about the format can be found here

http://www.w3.org/TR/NOTE-datetime

Below code does the conversion


try

   {
       string date = "2012-07-23T18:30:02Z";
       string dateString = date.Replace("Z", string.Empty);
       DateTime value = DateTime.ParseExact(dateString, "s", null);
       Console.WriteLine(value.ToString( "yyyy-MM-dd HH:mm:ss.fff"));
   }
   catch (Exception ex)
   {
       Console.WriteLine(ex.Message);
   }
   Console.Read();

No comments: