1

I want to generate the following DateTime format to XML:

2017-11-12T01:00:00-06:00

I am in the Eastern Standard Time zone which is -5 so when I do:

DateTime operatingDayLocal = DateTime.SpecifyKind(currentDate, DateTimeKind.Local);

the result is:

2017-11-12T01:00:00-05:00

I have to use the same DateTime format which is T00:00:00-00:00 but keeping the same time. I only want to change the offset of the DateTime so it can be generated to XML properly.

I tried this, but the time changes and the offset is gone:

DateTime operatingDayCentralTime = TimeZoneInfo.ConvertTime(currentDate, TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time"));

Which results to : 2017-11-12T00:00:00

Jean-François Beaulieu
  • 4,305
  • 22
  • 74
  • 107
  • 1
    How do you actually generate the XML ? – Liam Nov 10 '17 at 16:47
  • See https://stackoverflow.com/a/40093213/7556646 – Wollmich Nov 10 '17 at 16:48
  • 1
    It sounds like you should be using `DateTimeOffset` rather than `DateTime`, given that that's the information you want in the XML. – Jon Skeet Nov 10 '17 at 16:50
  • What do you actually mean by "generated to XML"? You're not showing anything in your question that is about XML. Could you please edit your question to include that code? – Matt Johnson-Pint Nov 10 '17 at 16:52
  • what is your expected result ? – Saif Nov 10 '17 at 16:59
  • Check out [Choosing between DateTime, DateTimeOffset, TimeSpan, and TimeZoneInfo](https://learn.microsoft.com/en-us/dotnet/standard/datetime/choosing-between-datetime). And then see [How can I XML Serialize a DateTimeOffset Property?](https://stackoverflow.com/q/3377036). – dbc Nov 10 '17 at 18:35
  • 1
    Simple : string operatingDayCentralTime = TimeZoneInfo.ConvertTime(currentDate, TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time")).ToString("yyyy-MM-ddThh:mm:ss-05:00"); – jdweng Nov 10 '17 at 19:46
  • I ended up changing the data type to a string instead of a DateTime. Then using the following code: `currentDate.ToString("o").Replace(".0000000-05:00", "-06:00");` – Jean-François Beaulieu Nov 13 '17 at 14:30

0 Answers0