none
DateTime.Now.ToString RRS feed

  • Question

  • Hi,

    I want to solve all the warnings and one warning is:

    Severity    Code    Description    Project    File    Line    Suppression StateWarning    CA1305    The behavior of 'DateTime.ToString(string)' could vary based on the current user's locale settings. Replace this call in 'Logging.LogStart(TextWriter)' with a call to 'DateTime.ToString(string, IFormatProvider)'. 


    This line gives the warning:

    string currentDateTime = DateTime.Now.ToString("dd-MM-yyyy HH:mm:ss");

    When i change it to:

    string currentDateTime = DateTime.Now.ToString("dd-MM-yyyy HH:mm:ss", CultureInfo.InvariantCulture);


    I get this warning:

    Severity	Code	Description	Project	File	Line	Suppression State
    Error	CS1061	'string' does not contain a definition for 'InvariantCulture' and no accessible extension method 'InvariantCulture' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?)
    

    i have this using directive:

    using System.Globalization;

    But is is not really used because it is greyed out??

    What am i misisng?

    string currentDateTime = DateTime.Now.ToString("dd-MM-yyyy HH:mm:ss");

    Thursday, October 3, 2019 7:30 PM

Answers

  • using System.Globalization;
    DateTime.Now.ToString("dd-MM-yyyy HH:mm:ss", CultureInfo.InvariantCulture);

    ... is technically correct.

    I think you still have a property with the name "CultureInfo" of the Type string in your class. With this you don't access System.Globalization.CultureInfo but the property.

    You can force the usage of the class with ...

    DateTime.Now.ToString("dd-MM-yyyy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture);

    Thursday, October 3, 2019 8:25 PM

All replies

  • Custom DateTime Formatting

    There are following custom format specifiers y (year), M (month), d (day), h (hour 12), H (hour 24), m (minute), s (second), f (second fraction), F (second fraction, trailing zeroes are trimmed), t (P.M or A.M) and z (time zone).

    Following examples demonstrate how are the format specifiers rewritten to the output.

    // create date time 2008-03-09 16:05:07.123
    			DateTime dt = new DateTime();
     
    			String.Format("{0:y yy yyy yyyy}", dt);  // "8 08 008 2008"   year
    			String.Format("{0:M MM MMM MMMM}", dt);  // "3 03 Mar March"  month
    			String.Format("{0:d dd ddd dddd}", dt);  // "9 09 Sun Sunday" day
    			String.Format("{0:h hh H HH}", dt);  // "4 04 16 16"      hour 12/24
    			String.Format("{0:m mm}", dt);  // "5 05"            minute
    			String.Format("{0:s ss}", dt);  // "7 07"            second
    			String.Format("{0:f ff fff ffff}", dt);  // "1 12 123 1230"   sec.fraction
    			String.Format("{0:F FF FFF FFFF}", dt);  // "1 12 123 123"    without zeroes
    			String.Format("{0:t tt}", dt);  // "P PM"            A.M. or P.M.
    			String.Format("{0:z zz zzz}", dt);  // "-6 -06 -06:00"   time zone


    Abu Ehab Developer

    Thursday, October 3, 2019 7:43 PM
  • Hi,

    Thanks, but that gives the same warning when i analyze the project.

    Severity	Code	Description	Project	File	Line	Suppression State
    Error	CS1061	'string' does not contain a definition for 'InvariantCulture' and no accessible extension method 'InvariantCulture' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?)


    Thursday, October 3, 2019 7:52 PM
  • Found it. I had a property whith the "CultureInfo".
    Thursday, October 3, 2019 7:56 PM
  • Okay 

    Try this :

    DateTime dt = newDateTime();

    // month/day numbers without/with leading zeroes
    String.Format("{0:M/d/yyyy}", dt);            // "3/9/2008"
    String.Format("{0:MM/dd/yyyy}", dt);          // "03/09/2008"
    
    // day/month names
    String.Format("{0:ddd, MMM d, yyyy}", dt);    // "Sun, Mar 9, 2008"
    String.Format("{0:dddd, MMMM d, yyyy}", dt);  // "Sunday, March 9, 2008"
    
    // two/four digit year
    String.Format("{0:MM/dd/yy}", dt);            // "03/09/08"
    String.Format("{0:MM/dd/yyyy}", dt);          // "03/09/2008"

     


    Abu Ehab Developer

    Thursday, October 3, 2019 8:00 PM
  • using System.Globalization;
    DateTime.Now.ToString("dd-MM-yyyy HH:mm:ss", CultureInfo.InvariantCulture);

    ... is technically correct.

    I think you still have a property with the name "CultureInfo" of the Type string in your class. With this you don't access System.Globalization.CultureInfo but the property.

    You can force the usage of the class with ...

    DateTime.Now.ToString("dd-MM-yyyy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture);

    Thursday, October 3, 2019 8:25 PM
  • Hello,

    Working from this, say it was picked up from a posting on the web, 

    string currentDateTime = DateTime.Now.ToString("dd-MM-yyyy HH:mm:ss", InvariantCulture);

    InvariantCulture is lite up like a Christmas tree, you search as learn that a using statement is needed. 

    Clicking the link a using statement is inserted but still an error, this means someplace else in your code or another external namespace is conflicting so to test this apping the namespace as follows.

    string currentDateTime = DateTime.Now.ToString("dd-MM-yyyy HH:mm:ss", 
        System.Globalization.CultureInfo.InvariantCulture);


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thursday, October 3, 2019 8:45 PM
    Moderator