C# Date Formatting Is Losing Slash Separators

C# date formatting is losing slash separators

Console.WriteLine(DateTime.Now.ToString("ddd M/dd/yy", CultureInfo.InvariantCulture));
Console.ReadLine();

try the above

DateTime.ToString removes slashes

You might need to provide a culture for your to string.

DateTime.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture);

C# - How do i get a slash in DATETIME, with System.Globalization.CultureInfo

Yes, the / character has the specific meaning of "culture specific date separator" in custom date/time format strings.

If you want a literal /, you need to quote it in the pattern:

var culture = new CultureInfo("da-DK");
string formatted = datetime.ToString("ddd. dd'/'MM", culture);

Output on my machine:

to. 27/06

It's using "06" instead of "6" because you've used MM in your format string - if you don't want zero padding for the day and month numbers, use "ddd. d'/'M" as the format string. That doesn't help the "to" become "Tor", admittedly. If you want that, you'd need to modify the culture's abbreviated day-of-week values.

DateTime.ToString() does not work as expected with slash as date-separator

Your currrent culture's date-separator seems to be - that's why you get it. You have to specify InvariantCulture:

string dateString = dt.toString("MM/dd/yyyy HH:mm", CultureInfo.InvariantCulture);  

See: The "/" Custom Format Specifier

The "/" custom format specifier represents the date separator, which
is used to differentiate years, months, and days. The appropriate
localized date separator is retrieved from the
DateTimeFormatInfo.DateSeparator property of the current or specified
culture
.

Another way is to escape the / with \:

string dateString = dt.toString(@"MM\/dd\/yyyy HH\:mm");  

But in my opinion, if you already know the special meaning of / as "current culture's date-separator", it's better(in terms of readability) to use the correct CultureInfo (or InvariantCulture) instead.

unable to change date format with / as separator , if default system date format having - as date separator

The / character is a special formatting character for DateTime which says "use the specified/current locale's date separator".

If you want to force it to use / you have to escape the character by surrounding it with single quotes:

var date=string.Format("{0:dd'/'MM'/'yyyy}", DateTime.Now)

Alteratively, you can escape it with the \ character:

var date=string.Format("{0:dd\\/MM\\/yyyy}", DateTime.Now)

(But I find the first approach to be slightly more readable.)

Finally, you can also use double-quotes to escape it, but that's even less readable:

var date = string.Format("{0:dd\"/\"MM\"/\"yyyy}", DateTime.Now);

Alternatively (as Jon points out) you can override the culture to use one that uses / as a separator:

var date = string.Format(CultureInfo.InvariantCulture, "{0:dd/MM/yyyy}", DateTime.Now);

Why won't DateTime format with /?

Separators are culture dependent. Try providing custom separator. For current date it can be

 DateTime.Now.ToString("M'/'dd'/'yyyy")

For arbitrary myDate date

 myDate.ToString("M'/'dd'/'yyyy")

Why does DisplayFormat with DataFormatString changes / (slash) to - (dash)?

Use DataFormatString = @"{0:dd\/MM\/yyyy}" instead. Since the / identifies a character that should be replaced by the default date separator for the current culture, you need to escape it in order for it to be used as a literal in the format.

This way you have a fixed format instead of one that dynamically uses the date separator of the current culture.

An alternative to escape the / character can be: DataFormatString = "{0:dd'/'MM'/'yyyy}"

Override the default date separator in .net

Globalisation in ASP.NET should do everything for you pretty much. See this MSDN article, entitled How to: Set the Culture and UI Culture for ASP.NET Web Page Globalization. This should be exactly what you want, as you simply need to set the current (UI) culture for the current thread when the user logs in. You can then call date.ToString() and it will return the text representation in the correct format.

Equivalently, you could do something like this:

var culture = System.Globalization.CultureInfo.GetCultureInfo("en-GB");
var dateString = date.ToString(culture.DateTimeFormat);

But it's really just doing the same thing manually, and is far less elegant. You might as well make use of the ASP.NET globalisation framework here.

C# display / (forward slash) in MaskedTextBox

According to this page, a slash is a 'date separator' and therefore I'm guessing that your system currently runs in a locale where a date separator is a dash.

/ Date separator. The actual display character used will be the date symbol appropriate to the format provider, as determined by the control's FormatProvider property.

If you really DO want a forward slash then "00\/00\/00" should do the trick (but then it's not really a compliant date input).



Related Topics



Leave a reply



Submit