Datetime Tostring Issue with Formatting Months with "Mm" Specifier

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.

Issue DateTime.ToString with string format M in .NET

From The "M" Custom Format Specifier

If the "M" format specifier is used without other custom format
specifiers
, it is interpreted as the "M" standard date and time format
specifier. For more information about using a single format specifier,
see Using Single Custom Format Specifiers later in this topic.

From Using Single Custom Format Specifiers

A custom date and time format string consists of two or more
characters. Date and time formatting methods interpret any
single-character string as a standard date and time format string. If
they do not recognize the character as a valid format specifier, they
throw a FormatException. For example, a format string that consists
only of the specifier "h" is interpreted as a standard date and time
format string. However, in this particular case, an exception is
thrown because there is no "h" standard date and time format
specifier.

To use any of the custom date and time format specifiers as the only
specifier in a format string (that is, to use the "d", "f", "F", "g",
"h", "H", "K", "m", "M", "s", "t", "y", "z", ":", or "/" custom format
specifier by itself), include a space before or after the specifier,
or include a percent ("%") format specifier before the single custom
date and time specifier
.

That's why you can use one of these;

Console.WriteLine(DateTime.Now.ToString(" M")); // 7
Console.WriteLine(DateTime.Now.ToString("M ")); //7
Console.WriteLine(DateTime.Now.ToString("%M")); //7

Date Changes After Formatting It to YYYY-MM-DD In C#

mm stands for Minute

you have to use MM

CurrentDate.ToString("yyyy-MM-dd");

Datetime.now month is incorrect in c#

The format mm stands for minute. Change it to

DateTime.Now.ToString("MM-dd-yyyy")

C# DateTime to YYYYMMDDHHMMSS format

DateTime.Now.ToString("yyyyMMddHHmmss"); // case sensitive

Getting Wrong DateTime Format

You could use DateTime.ToString with CultureInfo.InvariantCulture instead:

var dateString = date.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture);

The reason why / is replaced with . is that / is a 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.

So either use InvariantCulture which uses / as date separator or - more appropriate - escape this format specifier by embedding it within ':

var dateString = date.ToString("dd'/'MM'/'yyyy");

Why this is more appropriate? Because you can still apply the local culture, f.e. if you want to output the month names, but you force / as date separator anyway.

DateTime.ToString formatting

How about:

string.Format(ci, "{0:ddd} {0:d}", x)

DateTime.ToString() format that can be used in a filename or extension?

You can use this:

DateTime.Now.ToString("yyyy-dd-M--HH-mm-ss");


Related Topics



Leave a reply



Submit