Search code examples
c#datetimeazureformatdatetime

Accurate DateTime string format


I have a DateTime variable that holds the following value: 5/11/2014 7:56:26 am

I am currently using the following code to format this as a string: uDate.ToString("s"). Using this code, I get the following value: 2014-11-05T07:56:26

I need this to be more accurate. The exact value I am wanting to get is in the following range of accuracy: 2014-11-05T07:56:26.4

I have done some research at the following link: http://www.w3.org/TR/NOTE-datetime

Here is what I have found:

This profile does not specify how many digits may be used to represent the decimal fraction of a second. An adopting standard that permits fractions of a second must specify both the minimum number of digits (a number greater than or equal to one) and the maximum number of digits (the maximum may be stated to be "unlimited").

The value I am wanting to get is the value used on Azure when displaying a DateTime variable in a web service request.

How can I format my own DateTime strings so that they are the same format as the format used on Azure? Is there a specific format that Azure uses?

Basically, how can I format a DateTime string to be more accurate than the code: uDate.ToString("s")

Thanks in advance.


Solution

  • I believe the most "complete" information would be to use the "O" value, including fractions and also TimeZone (when available).

    var myDate = DateTime.Now.ToString("o");
    

    See also this article. Microsoft states that "complies with ISO 8601" and every .Net platform (also Azure) is able to read this string as DateTime, and also properly set the 'DateTimeKind' when available.