How does CultureInfo.InvariantCulture and DateTime to String works

19,346

Solution 1

For your particular use case, specifying the culture explicitly as CultureInfo.InvariantCulture isn't absolutely necessary.

For the format string "yyyyMMdd", all of the components will output just integer characters, and happen to be unaffected by culture. Using CultureInfo.InvariantCulture might be a small performance improvement (avoids having to lookup the current thread's culture), and isn't a bad thing to get in the habit of if you want to develop against very specific information. It would only be an issue if you were using "ddd" or anything else that involves the name of the month, day, etc. (or simply using .ToString() without parameters, which would have the entire thing determined by the current thread's culture)

That said, let's look at the other issue here: you're creating strings to store in your database. Why is this being done here rather than in the presentation layer? Are you not using parameterized queries?

Please, please please use parameterized queries. See How and Why to Use Parameterized Queries

Solution 2

As you mentioned in comments, you have a datetime field and you are going to have a character representation of that. In this case I suggest you use a computed column and do everything in database level.

Share:
19,346
Maximus Decimus
Author by

Maximus Decimus

Updated on June 24, 2022

Comments

  • Maximus Decimus
    Maximus Decimus almost 2 years

    I've been looking for information on Internet about this optional parameter CultureInfo.InvariantCulture when formatting in .NET

    These articles explains about it: What does cultureinfo invariantculture mean , What is the invariant culture. But it's still not clear for me.

    I want to manage and save a datetime variable as this format 'yyyyMMdd' and I formatted it like this:

    DateTime localDateTime = DateTime.Today; //> 2016-02-10 12:33
    string formattedDateTime = localDateTime.ToString("yyyyMMdd"); //> 20160210
    

    It is suppossed to have a varchar column in db with a length of 8. But in a specific case it started to save 7 chars. I'm waiting to get access to our client's domain to check the saved format.

    However, each user can set no matter what culture in the system (e.g. "en", "es", "fr"), so even if the method ToString which is forced to format like this "YYYYMMDD", do I need to set the invariant culture? If not, I could risk to have my datetime in string with another format? Does the regional configuration in database server matter?

    This is that I'm attempting to do right now, but not sure if this will fix the problem after we check in tests:

    DateTime localDateTime = DateTime.Today;
    string formattedDateTime = localDateTime.ToString("yyyyMMdd", CultureInfo.InvariantCulture); //> 20160210
    

    This is another scope, if I need to build my variable from database and save it in a stored procedure, I'm doing this:

    CONVERT(varchar, getdate(), 112) --ISO for yyyymmdd
    

    EDIT:

    We did some tests:

    DateTime currentDateTime = dateParameter; // 10/2/16 12:33 PM
    string dateParameterAsString = dateParameter.ToString(); // "10/2/16 12:33 PM"
    string formattedIdentifier = dateParameter.ToString(System.Globalization.CultureInfo.InvariantCulture); // "02/10/2016 12:33:00"
    string formattedIdentifier2 = dateParamter.ToString("yyyyMMdd"); // "20160218"
    string formattedIdentifier3 = dateParamter.ToString("yyyyMMdd", System.Globalization.CultureInfo.InvariantCulture); // "20160218"
    
  • Maximus Decimus
    Maximus Decimus about 8 years
    Thanks for your valuable answer. It's not calculated at presentation layer. It's done within a service library which receives all the parameters and once calculated, all the data is managed with a ORM library that uses SqlParameters inside. We're analyzing at this moment the service method which performs this calculation. I'm afraid that in cases where the datetime is 2016-02-01, it is being saved as 201621 (6 chars)