Delete start zero month of C # form

I am having trouble removing the leading zero from the date I found on the miscrosoft website.

DateTime date1 = new DateTime(2008, 8, 18); Console.WriteLine(date1.ToString("(M) MMM, MMMM", CultureInfo.CreateSpecificCulture("en-US"))); // Displays (8) Aug, August 

Totally not working here.

This is my code:

 string date = '2013-04-01' DateTime billrunDate = Convert.ToDateTime(date); string test = billrunDate.ToString("M"); 

Now test 01 April

I just need it to be 4 per line or int idc Thanks!

Change if I do:

 billrunDate.ToString("(M)"); 

I get (4) but I don't need ()

EDIT 2: Well, it works

 string test = billrunDate.ToString(" M "); string testTwo = test.Trim(); 

Very ugly

+6
source share
3 answers

He interprets M as the standard date and time format for the "day of the month template".

To interpret a single character pattern as a custom date and time pattern , just prefix it with % :

 string test = billrunDate.ToString("%M"); 
+17
source

One of my most MSDN reference pages is Custom Date and Time Strings . You can use them as part of the formatting passed to the ToString () method. If any of them are standard formatting patterns (like "M"), and you want to use them together, you should preface them with "%" or have a space before or after them in the format string (so use "% M", “M” or “M” instead of “M”).

Relevant Section:

"M"

Month, from 1 to 12.

"MM"

Month, from 01 to 12.

MMM

The abbreviated name of the month.

"MMMM"

The full name of the month.

+3
source

You do not need to convert to enter a date to get the month number.
Just read the Month property of the DateTime class:

 string date = "2013-04-01"; DateTime billrunDate = Convert.ToDateTime(date); string test = billrunDate.Month.ToString(); 
+3
source

All Articles