What format do I use for Date/Time when writing to an XML file using .NET? Do I simply use DateTime.ToString()
, or do I have to use a specific format?
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");
The international standard recommends writing the date as year, then month, then the day: YYYY-MM-DD. So if both Australians and Americans used this, they would both write the date as 2019-02-03. Writing the date this way avoids confusion by placing the year first.
Lexical form The lexical form of xs:dateTime is a finite-length sequence of characters of the following form: yyyy - mm - dd T hh : mm : ss . ssssssssssss zzzzzz . The following abbreviations describe this form: yyyy. A four-digit numeral that represents the year.
I always use the ISO 8601 format (e.g. 2008-10-31T15:07:38.6875000-05:00
) -- date.ToString("o")
. It is the XSD date format as well. That is the preferred format and a Standard Date and Time Format string, although you can use a manual format string if necessary if you don't want the 'T' between the date and time: date.ToString("yyyy-MM-dd HH:mm:ss");
EDIT: If you are using a generated class from an XSD or Web Service, you can just assign the DateTime instance directly to the class property. If you are writing XML text, then use the above.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With