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"