Using XMLSchema dateTime formatted DateTime’s in .NET

This is here mostly for my benefit as I keep forgetting it, but this stuff is all over the internet.

Consuming and producing xmlschema dateTime formatted values is straightforward enough when you know the specification (see the link), briefly it is;


The .NET class XmlConvert can be used to convert dateTime values between string and DateTime, using the ToString() and ToDateTime() methods.

   // convert from string to datetime
   var dt = XmlConvert.ToDateTime(dateString, XmlDateTimeSerializationMode.Utc);
   dt = XmlConvert.ToDateTime(dateString, XmlDateTimeSerializationMode.Unspecified);
   dt = XmlConvert.ToDateTime(dateString, XmlDateTimeSerializationMode.RoundtripKind);
   dt = XmlConvert.ToDateTime(dateString, XmlDateTimeSerializationMode.Local);

   // convert from datetime to string
   var dtStr = XmlConvert.ToString(dt, XmlDateTimeSerializationMode.Utc)
   dtStr = XmlConvert.ToString(dt, XmlDateTimeSerializationMode.Unspecified)
   dtStr = XmlConvert.ToString(dt, XmlDateTimeSerializationMode.RoundtripKind)
   dtStr = XmlConvert.ToString(dt, XmlDateTimeSerializationMode.Local)

Published by

Phil Harding

SharePoint Consultant, Developer, Father, Husband and Climber.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.