When is empty string "" a valid DateTime string format?

Neil Humby

Seeing a strange issue where on some systems the below code steps into the if statement (i.e. it returns true) while in other systems it returns false and steps into the else statement. What environmental conditions or framework version changes am I missing where this was changed? For example .net Fiddle returns true, but my own console apps return false.

    DateTime time;
    formatText = "";
    if (DateTime.TryParse (DateTime.Now.ToString(formatText), CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal, out time))
    {
        // If TryParseExact Worked
        Console.WriteLine ("True: " + time.ToString ()); 
    }
    else
    {
        // If TryParseExact Failed
        Console.WriteLine ("Failed to Parse Date");
    } 
Zohar Peled

String representations of DateTime are culture specific.

Passing an empty string or null as the format parameter of the ToString overload of DateTime is the same as passing the standard format specifier "G" - from the remarks section of the DateTime.ToString Method (String) msdn page:

If format is null or an empty string, the general format specifier, 'G', is used.

The TryParse overload you are using attempts to parse the DateTime value using the date and time formats available in the IFormatProvider format parameter - InvariantCulture in your case - so when you use TryParse with InvariantCulture, unless your current culture's ShortDatePattern and LongTimePattern properties are the same as in InvariantCulture, the tryParse will fail.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Return empty string for no match on DateTime

分類Dev

Validate is a string is empty using Scanner and return the empty string as valid value

分類Dev

Format DateTime to local string with a timezone

分類Dev

Format DateTime without string conversion

分類Dev

string was not recognized as a valid Datetime on Windows Server 2016

分類Dev

Format/covert JS object string to valid JSON format

分類Dev

Replace string value with '0' when string is empty

分類Dev

Getting an empty string when scanned

分類Dev

String.Format()TimeSpanとDateTime

分類Dev

How to convert string data with variable format to datetime?

分類Dev

String.format() DateTime With Arabic culture

分類Dev

android string datetime format with month and day

分類Dev

Using String.Format when passing DateTime arguments type to DB. Exception is thrown. What is wrong?

分類Dev

How to reference the empty string key in the Format String Syntax?

分類Dev

Convert a specific format dateTime string to DateTime in C#

分類Dev

String.format、longおよびDateTime変換

分類Dev

What is datetime object in Google API, and how to format its string output?

分類Dev

Using boost parse datetime string: With single digit hour format

分類Dev

String.format()DateTimeアラブ文化

分類Dev

Seq performance hit when specifying a format string

分類Dev

Is String a valid Error type when it can be reported immediately in stdout?

分類Dev

String to string format

分類Dev

String Format .format to variable

分類Dev

string::erase(0) on an empty string?

分類Dev

javascript datetime to string and back to datetime?

分類Dev

Convert empty dictionary to empty string

分類Dev

Convert DateTime String to Date-only when listing in comboBox

分類Dev

Verify an element using getText() returns empty string when using typeahead

分類Dev

Deserializing JSON string fails when JSON dictionary is empty

Related 関連記事

  1. 1

    Return empty string for no match on DateTime

  2. 2

    Validate is a string is empty using Scanner and return the empty string as valid value

  3. 3

    Format DateTime to local string with a timezone

  4. 4

    Format DateTime without string conversion

  5. 5

    string was not recognized as a valid Datetime on Windows Server 2016

  6. 6

    Format/covert JS object string to valid JSON format

  7. 7

    Replace string value with '0' when string is empty

  8. 8

    Getting an empty string when scanned

  9. 9

    String.Format()TimeSpanとDateTime

  10. 10

    How to convert string data with variable format to datetime?

  11. 11

    String.format() DateTime With Arabic culture

  12. 12

    android string datetime format with month and day

  13. 13

    Using String.Format when passing DateTime arguments type to DB. Exception is thrown. What is wrong?

  14. 14

    How to reference the empty string key in the Format String Syntax?

  15. 15

    Convert a specific format dateTime string to DateTime in C#

  16. 16

    String.format、longおよびDateTime変換

  17. 17

    What is datetime object in Google API, and how to format its string output?

  18. 18

    Using boost parse datetime string: With single digit hour format

  19. 19

    String.format()DateTimeアラブ文化

  20. 20

    Seq performance hit when specifying a format string

  21. 21

    Is String a valid Error type when it can be reported immediately in stdout?

  22. 22

    String to string format

  23. 23

    String Format .format to variable

  24. 24

    string::erase(0) on an empty string?

  25. 25

    javascript datetime to string and back to datetime?

  26. 26

    Convert empty dictionary to empty string

  27. 27

    Convert DateTime String to Date-only when listing in comboBox

  28. 28

    Verify an element using getText() returns empty string when using typeahead

  29. 29

    Deserializing JSON string fails when JSON dictionary is empty

ホットタグ

アーカイブ