From msdn it seems like I can create my own format with Datetime.ToString() method by using M, m, d, y etc. But when I tried one it didn't worked as expected, snipped below is the issue.
I was expecting 7/29/2015 but received 7-29-2015 !!! why?
Looks like your DateSeparator of your CurrentCulture is - and that's why / character replace itself to it.
"/" custom format specifier has a special meaning as replace me with current culture or supplied culture date separator.
You have a few options, you either escape it with single quotes (or \/ in a verbatim string literal) or use a culture that has / as a DateSeparator like InvariantCulture.
string s = DateTime.Now.ToString("M'/'d'/'yyyy");
string s = DateTime.Now.ToString(#"M\/d\/yyyy");
string s = DateTime.Now.ToString("M/d/yyyy", CultureInfo.InvariantCulture);
Related
I write txtChallanDate.Text = DateTime.Now.ToString("dd/MM/yyyy");
It shows ok, but after publishing in server (IIS), the date shows in dd.mm.yyyy format
DateTime.Now.ToString("dd'/'MM'/'yyyy")
/ is a separator character and the replaced character in ToString culture dependent. If you want the literal / character you must escape it with single quotes. Alternatively you can supply a CultureInfo instance to the ToString method.
Example Stackblitz
From msdn it seems like I can create my own format with Datetime.ToString() method by using M, m, d, y etc. But when I tried one it didn't worked as expected, snipped below is the issue.
I was expecting 7/29/2015 but received 7-29-2015 !!! why?
Looks like your DateSeparator of your CurrentCulture is - and that's why / character replace itself to it.
"/" custom format specifier has a special meaning as replace me with current culture or supplied culture date separator.
You have a few options, you either escape it with single quotes (or \/ in a verbatim string literal) or use a culture that has / as a DateSeparator like InvariantCulture.
string s = DateTime.Now.ToString("M'/'d'/'yyyy");
string s = DateTime.Now.ToString(#"M\/d\/yyyy");
string s = DateTime.Now.ToString("M/d/yyyy", CultureInfo.InvariantCulture);
What property in CultureInfo.InvariantCulture helps to discard the functionality of "/".
I am having this problem because I have my customCulture.
and when I use it to format DateTime it replaces "/" with the culture Specific separator.
So I want to know what property if I change in my customCulture the functionality of "/" will be discarded.
Let me clear my problem,
I want to export some data to a file with DateTime as specified by user.
I am creating customCulture as follows
CultureInfo customCulture = new CultureInfo(CultureInfo.CurrentCulture.Name);
I am taking the date format from the user and injecting it into my CustomCulture object as
customCulture.DateTimeFormat.ShortDatePattern = MyClass.DateFormatFromUser;
Now when I want to convert this to String pattern. I am doing it as follows,
string.Format(customCulture, "{0:G}", dateTimeValue);
Now the problem is String.Format replaces any "/" present in ShortDatePattern with customCulture.DateTimeFormat.DateSeparator
For example if he user gives the format as
dd/MM/yyyy
and my current culture is faeroese ie., {fo-FO}
For these I get output as 25-09-1990.
If you want to change your custom culture so that the date separator is the slash (/) then you are looking for the DateTimeFormatInfo.DateSeparator property:
customCulture.DateTimeFormat.DateSeparator = "/";
Use the escape character \: change every / into \/ when you don't want it to be replaced with culture separator.
DateTimeFormatInfo.DateSeparator is the culture-specific separator that separates the components of a date.
What you ask is not possible and Marcin Juraszek is right. There is a misunderstanding of how the format specifier and the CultureInfo work together.
The / character is a format specifier just like m,D,y,Y and all the other date format specifiers. That's just the syntax of the format string, it has nothing to do with culture and can't change. You can't change the meaning of '/' any more than you can change the meaning of the other specifiers
String.Format uses this specifier to find where it should place the CultureInfo's DateSeparator character when formatting a date.
It doesn't matter whether you include the specifier directly in a format string like String.Format("{0:dd/MM/yyyy}",DateTime.Now) or you pass it indirectly through CultureInfo.DateTimeFormat.ShortDatePattern like you tried, it will always be replaced by the DateSeparator character.
Your only option is to escape the character either by prepending it with '\' or surrounding it with ' ' as cremor suggested, eg.
customCulture.DateTimeFormat.ShortDatePattern = "dd\\/MM/yyyy";
or
customCulture.DateTimeFormat.ShortDatePattern = "dd'/'MM/yyyy";
So, if you wanted to create a string like 11-07/2013 you would have to write something like this:
CultureInfo customCulture = new CultureInfo(CultureInfo.CurrentCulture.Name);
customCulture.DateTimeFormat.ShortDatePattern = "dd/MM\\/yyyy";
customCulture.DateTimeFormat.DateSeparator = "-";
var dateTimeValue = DateTime.Now;
var result = string.Format(customCulture,"{0:d}", dateTimeValue);
Console.WriteLine(result);
I have also tried shielding the '/' symbol in the formatting string, but it didn't quite work. My final goal is to get the date with the '/' symbols as separators. I guess I can use DateTime.ToString(“dd/MM/yyyy”).Replace('.', '/'), but that feels a bit excessive.
The / character in date/time format strings stands for "whatever the date separator of the format provider is". Since you do not supply a format provider Thread.CurrentCulture is used, and in your case the current culture uses . as the date separator.
If you want to use a literal slash, place it inside single quotes:
dateTime.ToString("dd'/'MM'/'yyyy");
Alternatively, you could specify a format provider where the date separator is /:
dateTime.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture);
All of the above is documented on MSDN.
See the difference in a live example.
string s = dt.ToString("dd/M/yyyy", CultureInfo.InvariantCulture)
This is because of the way ToString works by default, in accordance with the current culture:
This method uses formatting information derived from the current
culture.
So, override that:
string date = dt.ToString("dd/M/yyyy", CultureInfo.InvariantCulture)
This works (note the InvariantCulture):
DateTime.Now.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture)
If a CultureInfo is not specified, the CurrentCulture will be used. If this is a culture that doesn't use slashes as separators in dates it is replaced by whatever the actual culture date separator is.
This is driving me crazy. I have the following string in a ASP.NET 2.0 WebForm Page
string s = "0.009";
Simple enough. Now, if my culture is Spanish - which is "es-ES" - and I try to convert the string to Double, I do the following:
double d = Double.Parse(s, new CultureInfo("es-ES"));
what I'd expect is 0,009. Instead, I get 9. I understand that .NET thinks it is a thousand separator, which in en-US is a comma, but shouldn't it take the culture info I'm passing to the parse method and apply the correct format to the conversion?
If I do
double d = 0.009D;
string formatted = d.ToString(new CultureInfo("es-ES"));
formatted is now 0,009. Anybody?
It is taking the culture you gave and applying the correct formatting. You provided a string of "0.009" and told it that it was Spanish...then you complain that it properly interpreted it as Spanish! Don't tell it that the string is Spanish when you know it isn't.
You should pass the Parse method the culture of the string being parsed, which in this case would be en-US or en-Gb or InvariantCulture.
what Jess's writing works for me. just for anyone who'd need to try out how to get "invariant culture": it looks this
double d = Double.Parse(myString, CultureInfo.InvariantCulture);
(first stackoverflow post, so yea, rather marginal ;)
You have it backwards.
When you say double d = Double.Parse(s, new CultureInfo("es-ES"));, you are asking .NET to parse your string into a double, assuming that the string is written in the es-ES culture.
In Spanish culture, "." is a thousands separator, so "0.009" is 9.
When you convert using ToString(), at the end, it's saying convert 0.009 to a string using the spanish culture, so it uses "," as the decimal separator, and you get "0,009". The behavior is correct.
My guess is that you want to use Double.Parse with the Invariant Culture, and ToString with the spanish culture, so 0.009 becomes 0,009.
I think you are interpreting it the wrong way around, in es-ES culture 0.009 is really just a long way of saying 9, as the "." is not the decimal separator, so if you ask for the string "0.009" to be parsed with the es-ES culture you should indeed get the deouble 9.0. If you ask it to parse "0,009" you should get a double of 0.009.
Similarly, if you ask it to format the double 0.009 you should get the string "0,009" in es-ES.
You're mistaking parsing and formatting. You get 9 instead of .009 the first time because you take a string that is formated in a .-based culture and parse it using a ,-based culture. You need to parse it using whatever culture it was created with and then format it using whatever culture you want for display.
double d = Double.Parse("0,009",
NumberStyles.AllowDecimalPoint | NumberStyles.AllowThousands,
CultureInfo.CreateSpecificCulture("es-ES"));
In es-ES culture "," is a decimal seporator (not ".")