Click here to Skip to main content
15,895,667 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
if we display current date in the form of
label1.text=datetime.now.tostring();

then we get 3/18/2011 12:01:10 PM
I want to display previous month 21 st date ,How?

Label2.text=?
Means 2/21/2011 12:01:10 Pm How?


please send the c# Code Urgently Fast
Posted
Updated 18-Mar-11 6:01am
v6

You can use DateTime Constructor's 6th overload

DateTime dt=new DateTime(2011,2,21,12,1,10);
Label1.Text=dt.ToString();
 
Share this answer
 
Comments
Raj.rcr 18-Mar-11 4:03am    
Good call. my 5
Sajid Ahmed Shahsroha 19-Mar-11 7:55am    
whoever downvoted me plz could you tell me why ?? so that i can improve myself
[no name] 19-Mar-11 9:15am    
Hard coding the values may solve this immediate problem but does nothing to provide a solution that will work in all cases.
You can use the appropriate constructor of the DateTime[^] structure and display it.

You are not mentioning about any rules for deriving the date. If there is any use the AddDays method accordingly.
 
Share this answer
 
DateTime date = DateTime.Now.AddMonths(-1);
DateTime date2 = new DateTime(date.Year, date.Month, 21, date.Hour, date.Minute, date.Second);
label1.Text = date2.ToString();

or alternatively:
DateTime now = DateTime.Now;
DateTime date = now.AddMonths(-1);
int days = DateTime.DaysInMonth(date.Year, date.Month);
DateTime date2 = now.AddDays(-days - now.Day + 21);
label1.Text = date2.ToString();
 
Share this answer
 
v2
Comments
Toniyo Jackson 18-Mar-11 3:07am    
Good answer. +5

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