Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate datetime format for XML

Tags:

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"> 
like image 437
Nil Pun Avatar asked Jun 11 '11 04:06

Nil Pun


People also ask

What is the DateTime format in XML?

The dateTime is specified in the following form "YYYY-MM-DDThh:mm:ss" where: YYYY indicates the year. MM indicates the month. DD indicates the day.

How can I change the date format in XML?

Can use the following conversion code to convert UTC format string to any other DateTime format. string result = Convert. ToDateTime("2011-02-04T00:00:00+05:30"). ToString("MM/dd/yyyy h:mm:ss tt");

What is XSD DateTime format?

The standard XSD DateTime and Date formats are CCYY-MM-DDThh:mm:ss and CCYY-MM-DD, respectively, because the underlying XSD schema of the DataSet maps the DateTime and Date columns of the database to the DateTime and XSD Date data types.

What is XML data format?

What is XML? The Extensible Markup Language (XML) is a simple text-based format for representing structured information: documents, data, configuration, books, transactions, invoices, and much more. It was derived from an older standard format called SGML (ISO 8879), in order to be more suitable for Web use.


2 Answers

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"); 
like image 110
Oded Avatar answered Nov 06 '22 05:11

Oded


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.

like image 41
Haukman Avatar answered Nov 06 '22 06:11

Haukman