sharptooth
sharptooth

Reputation: 170489

How do I convert DateTime .NET datatype to W3C XML DateTime data type string and back?

I have a System.DateTime object and I need to convert it into a string storing that datetime in W3C XML DateTime format (yyyy-mm-ddThh:mm:ssZ) and then be able to convert the resulting string back into System.DateTime.

Is there something ready for that in .NET or do I have to implement it myself?

Upvotes: 22

Views: 17653

Answers (5)

RichB
RichB

Reputation: 520

If you want a date in the W3C XML format, then use .Net's W3C XML formatting API (been there since before v1.0):

var w3cStringFormat = XmlConvert.ToString(DateTime.Now);

results in:

2014-09-12T16:03:22.6833035+01:00

Then to get it back again:

var dateTime = XmlConvert.ToDateTime(w3cStringFormat);

Upvotes: 15

Waqas Raja
Waqas Raja

Reputation: 10872

Xml Serialization is there for you. Below is the code how to:-

DateTime someDateTime = DateTime.Now.AddDays(5);
System.Xml.Serialization.XmlSerializer ser = new System.Xml.Serialization.XmlSerializer(typeof(DateTime));
System.IO.MemoryStream ms = new System.IO.MemoryStream();
// converting to w3c xml
ser.Serialize(ms, someDateTime);

//**Edited**
ms.Position = 0;
//**Edited**

System.IO.StreamReader sr = new System.IO.StreamReader(ms);
string w3cxml = sr.ReadToEnd();

sr.Close();
ms.Close();

// doing reverse
System.IO.StringReader reader = new System.IO.StringReader(w3cxml);

DateTime thatDateTime = (DateTime)ser.Deserialize(reader);

reader.Close();

Upvotes: 0

Cheeso
Cheeso

Reputation: 192467

I thought W3C dateTime had a lot more significant digits for the time. Here's what I use:

// DateTime to W3C dateTime string
string formatString= "yyyy-MM-ddTHH:mm:ss.fffffffzzz";
dateTimeField.ToString(formatString) ;

// W3C dateTime string to DateTime 
System.Globalization.CultureInfo cInfo= new System.Globalization.CultureInfo("en-US", true);
dateTimeField= System.DateTime.ParseExact(stringValue, formatString, cInfo);

Upvotes: 21

Karg
Karg

Reputation: 1527

This will convert to the string you need and parse back the string to the DateTime:

var now = DateTime.Now;
Console.WriteLine(now.ToUniversalTime().ToString());
var nowString = now.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ssZ");
Console.WriteLine(nowString);

var nowAgain = DateTime.ParseExact(nowString, "yyyy-MM-ddTHH:mm:ssZ", null);
Console.WriteLine(nowAgain.ToUniversalTime().ToString());

Console.ReadLine();

Upvotes: 5

Jakub Konecki
Jakub Konecki

Reputation: 46008

I would think JSON.net can handle it. Use IsoDateTimeConverter.

From documentation:

public class LogEntry
{
  public string Details { get; set; }
  public DateTime LogDate { get; set; }
}

[Test]
public void WriteJsonDates()
{
  LogEntry entry = new LogEntry
  {
    LogDate = new DateTime(2009, 2, 15, 0, 0, 0, DateTimeKind.Utc),
    Details = "Application started."
  };

  string defaultJson = JsonConvert.SerializeObject(entry);
  // {"Details":"Application started.","LogDate":"\/Date(1234656000000)\/"}

  string javascriptJson = JsonConvert.SerializeObject(entry, new JavaScriptDateTimeConverter());
  // {"Details":"Application started.","LogDate":new Date(1234656000000)}

  string isoJson = JsonConvert.SerializeObject(entry, new IsoDateTimeConverter());
  // {"Details":"Application started.","LogDate":"2009-02-15T00:00:00Z"}
}

Upvotes: 0

Related Questions