C# DateTime - RFC 3339 format

cweston picture cweston · Feb 16, 2011 · Viewed 13.3k times · Source

I am trying to generate RFC 3339 compliant date strings (ie. '2008-03-19T00:00:00.0000000-04:00') however I seem to be having an issue with the offset being invalid. I am using the following:

private string GetDate(DateTime DateTime)
{
    DateTime UtcDateTime = TimeZoneInfo.ConvertTimeToUtc(DateTime);
    return XmlConvert.ToString(UtcDateTime, XmlDateTimeSerializationMode.Utc);
}

but this returns me with a value such as "1977-02-03T05:00:00Z"

I have also attempted using a specific format such as

 utcDateTime.ToString("yyyy-MM-dd'T'HH:mm:ss.fffK", DateTimeFormatInfo.InvariantInfo); 

But with the same results.


See this existing reference: How do I parse and convert DateTime's to the RFC 3339 date-time format?

Answer

CodesInChaos picture CodesInChaos · Feb 16, 2011

You are converting your data to UTC, so its timezone offset to UTC is 0:00. The RFC defines a convenient notation for UTC dates, the suffix Z. So this looks like a valid data-string to me.