Generate datetime format for XML

前端 未结 4 610
刺人心
刺人心 2021-01-01 15:03

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.

相关标签:
4条回答
  • 2021-01-01 15:12

    You can use ToString method

    DateTime time = DateTime.Now;              
    string format = "MMM ffffd d HH:mm yyyy";   // or any format you want  
    Console.WriteLine(time.ToString(format));
    
    0 讨论(0)
  • 2021-01-01 15:16

    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");
    
    0 讨论(0)
  • 2021-01-01 15:16

    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.

    0 讨论(0)
  • 2021-01-01 15:29

    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>
    
    0 讨论(0)
提交回复
热议问题