Nil Pun
Nil Pun

Reputation: 17383

Generate datetime format for XML

I'm trying to generate timestamp for cXML as shown below. Is there any function in C# which I can use to format date time to: 2011-06-09T16:37:17+16:37

e.g.

<cXML payloadID="[email protected]"
timestamp="2011-06-09T16:37:17+16:37">

Upvotes: 22

Views: 57491

Answers (4)

Haukman
Haukman

Reputation: 3776

Yes, using DateTime.ToString("s"), see this link: Standard Date and Time Format Strings. Be aware that "s" does not include the timezone information, whereas "o" does include both fractional seconds and timezone.

You can also use the XmlConvert.ToString method, where you can specify the time zone information as well.

Upvotes: 7

Oded
Oded

Reputation: 499382

Use the "o" format specifier - read about this one in the standard Date and Time format strings documentation on MSDN.

The pattern for this specifier reflects a defined standard (ISO 8601).

And:

6/15/2009 1:45:30 PM -> 2009-06-15T13:45:30.0900000

string formatted = DateTime.Now.ToString("o");

If this is not what you want, you will need to use a custom format string - I believe you will need to do this, as the offset is not standard.

string formatted = DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ssK");

Upvotes: 41

Android3
Android3

Reputation:

The following is an example of a date declaration in a schema:

<xs:element name="start" type="xs:date"/>

An element in your document might look like this:

<start>2002-09-24</start>

To specify a time zone, you can either enter a date in UTC time by adding a "Z" behind the date:

<start>2002-09-24Z</start>

or you can specify an offset from the UTC time by adding a positive or negative time behind the date:

<start>2002-09-24-06:00</start>

or

<start>2002-09-24+06:00</start>

Upvotes: 0

Abdallah
Abdallah

Reputation: 62

You can use ToString method

DateTime time = DateTime.Now;              
string format = "MMM ddd d HH:mm yyyy";   // or any format you want  
Console.WriteLine(time.ToString(format));

Upvotes: -1

Related Questions