Click here to Skip to main content
15,904,415 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
i am calculating the first day of every month for that code as follows

C#
DateTime now = DateTime.Now;
DateTime firstday = new DateTime(now.Year, now.Month, 1);
string day = firstday.Date.ToString();
Label1.Text = day;


Output as follows

7/1/2013 12:00:00 AM



I am getting the output correct, but i want the output as follows

01 Jul 2013


How can i write code using my above code?


Rgds,
Narasiman P.
Posted
Updated 3-Jul-13 3:40am
v2
Comments
Leo Chapiro 3-Jul-13 9:44am    
Take a look at the DateTime.ToString-Methode (String): http://msdn.microsoft.com/de-de/library/zdtaw1bw(v=vs.80).aspx
BiteForce 3-Jul-13 9:50am    
I think it works best with a switch-statement:

create a "day-string", a "month-string" and a "year-string";
then you can define, how the different substrings will be printed out.

for example:

Date date = new Date();
string day = null;
string month = null;
string year = date.Year;

switch(date.Day) {
case 1: day = "01";
case 2: day = "02";
//...
}

switch(date.Month) {
case 1: month = "Jan";
case 2: month = "Feb";
//...
}

Label1.Text = day + " " + month + " " + year;
Ron Beyer 3-Jul-13 11:02am    
I hope that's a joke...
BiteForce 3-Jul-13 14:42pm    
Uhm, I know, that it can be done much easier, I just wanted to mention another way of solving this problem...
I must admit, I also wanted to make some kind of joke, because there are still so many explanations for solving it, so I just came up with this idea...

1 solution

C#
label.Text = firstDay.ToString("dd MMM yyyy");


Custom DateTime Format Strings[^]
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900