How to parse and generate DateTime objects in ISO 8601 format
<p>There is this SOAP web service that sends me datetime objects in the following format</p>

<pre><code>2016-03-29T12:20:35.093-05:00
</code></pre>

<p>That is day 29 of March of year 2016. Hour: 12:20:35.093 (GMT-5).</p>

<p>I want to be able to create a <code>DateTime</code> object, like this:</p>

<pre><code>DateTime.Now
</code></pre>

<p>and get the string representation in the format described above and also the inverse operation, create a DateTime from a string like the one given above.</p>

<p>I've tried the following in order to create the date:</p>

<pre><code>new DateTime(2016, 3, 29, 12, 20, 35, 093, DateTimeKind.Utc)
</code></pre>

<p>However, I can't not see how to specifie GMT-5 there...</p>

<p>I don't know how to convert a DateTime to the specified string format, either.</p>

<p><strong>Using Nate's code I'm doing the following:</strong></p>

<pre><code>var d = new DateTimeOffset(2016, 3, 29, 12, 20, 35, 93, TimeSpan.FromHours(-3));

FormatIso8601(d)
</code></pre>

<p>However this call is returning: "2016-03-29T15:20:35Z" instead of :</p>

<pre><code>"2016-03-29T12:20:35.093-03:00"
</code></pre>

<p>which is what I actually need.</p>

<p>I think this works:</p>

<pre><code>        d.ToString("yyyy-MM-ddTHH:mm:ss.fffzzz")
</code></pre>