locked
Date format independent of the system datetime format RRS feed

  • Question

  • Hi All..

     

    My application (Windows - C#)  uses license which is in XML file, which stores the expiry date.

    If my System datetime format is different than what is stored in my XML file, it gives me error (Invalid DateTime string).

    I want my application to be independent of the system datetime format.

    Any help will be appreciated...

    Thanks

    Friday, August 31, 2007 10:44 AM

Answers

  • I agree with timvw; but I would recommend DateTime.TryParseExact unless you've validated the text beforehand.

     

    Alternatively, you could use

    Code Snippet

    DateTime.TryParse(text, CultureInfo.InvariantCulture, DateTimeStyles.None, out resultantDateTime);

     

     

    ... if you know your date string is always in EN-us format.  e.g. if your using something like

    Code Snippet

    DateTime.ToString(CultureInfo.InvariantCulture)

     

     

    to output datetime strings to your stream/file/etc. then using TryParse with CultureInfo.InvariantCulture would be the reverse of that.

    Friday, August 31, 2007 2:49 PM

All replies

  • Use ParseExact to read the string.. (And write with that same format string).
    Friday, August 31, 2007 12:40 PM
  • I agree with timvw; but I would recommend DateTime.TryParseExact unless you've validated the text beforehand.

     

    Alternatively, you could use

    Code Snippet

    DateTime.TryParse(text, CultureInfo.InvariantCulture, DateTimeStyles.None, out resultantDateTime);

     

     

    ... if you know your date string is always in EN-us format.  e.g. if your using something like

    Code Snippet

    DateTime.ToString(CultureInfo.InvariantCulture)

     

     

    to output datetime strings to your stream/file/etc. then using TryParse with CultureInfo.InvariantCulture would be the reverse of that.

    Friday, August 31, 2007 2:49 PM