Loading ...
Sorry, an error occurred while loading the content.

MSDN Visual Studio Beta 2 recommends ISO-8601 for text storage

Expand Messages
  • hjwoudenberg@aol.com
    What is the Recommended Way to Store a DateTime in Text? Most of the standard formats for the DateTIme do not preserve all the information in the DateTime.
    Message 1 of 1 , Aug 27, 2005
    • 0 Attachment

      What is the Recommended Way to Store a DateTime in Text?

      Most of the standard formats for the DateTIme do not preserve all the information in the DateTime. The also vary from one culture to another. Thus, the recommended format for storing a typical DateTime is like so:

      [C#]
      public string StoreDateTime(DateTime value) {
          // e.g 2003-10-26T14:33:41.1234567
          return DateTime.ToString("yyyy-MM-ddTHH:mm:ss.fffffff",
                                   CultureInfo.InvariantCulture);
      }

      public DateTime ReadDateTime(String value) {
          return DateTime.Parse(value, CultureInfo.InvariantCulture);
      }


      This format is useful, both because it is a standardized format, used in XML among other standards, and because with the 7 decimal places it retains all the precision of the DateTime.

      There is a critical piece of information that is lost here, which is the context of the time zone of the DateTime, if it is available. If you are communicating with a 3rd party system, or if your application has writers and readers on different time zones, it is recommended to use slightly different formats.

      If you handle your Dates in Universal time, use this format:

      [C#]
      public string StoreDateTimeUtc(DateTime value) {
          // e.g 2003-10-26T14:33:41.1234567Z
          return DateTime.ToString("yyyy-MM-ddTHH:mm:ss.fffffffZ",
                                   CultureInfo.InvariantCulture);
      }


      public DateTime ReadDateTimeUtc(String value) {
          return DateTime.Parse(value, CultureInfo.InvariantCulture,
                                DateTimeStyles.AjdustToUniversal);
      }
       

      Note that it is important to pass AdjustToUniversal to this routine, because the output format identifies the time as UTC, and converted to local time by default by Parse if they have any sort of time zone marker.

      If you handle your Dates in local time, use this format:

      [C#]
      public string StoreDateTimeLocal(DateTime value) {
          // e.g 2003-10-26T14:33:41.1234567-07:00
          return DateTime.ToString("yyyy-MM-ddTHH:mm:ss.fffffffzzz",
                                   CultureInfo.InvariantCulture);
      }
      public DateTime ReadDateTimeLocal(String value) {
          return DateTime.Parse(value, CultureInfo.InvariantCulture);
      }

      Note that in this case that if the reader and writer are in different time zones, Parse will convert the result to the local time zone by default.

      Beware: do NOT use a UTC format to store a local time or a Local format to store a UTC time. It will actually seem to work at first, but it will not correctly adjust when time zones change, and 3rd party systems reading the information will get the wrong time.

    Your message has been successfully submitted and would be delivered to recipients shortly.