Click here to Skip to main content
15,917,793 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hai friends
i have a doubt in datetimepicker tool in c#.

1.
I have to display remaining days in a selected date use only one datetimepicker.
for eg today date is 22-apr-2011. remaining days upto 31-mar-2012.
if it is jan, feb and mar it should display the days of current year
for eg 12-jan-2011 remaining days upto 31-mar-2011.

2.
Display the days of current month using datetimepicker tool in c#.
eg. if i am selecting jan month, the textbox should tells days = 31
Posted
Updated 22-Apr-11 1:54am
v2
Comments
BlackJack99 22-Apr-11 8:08am    
2nd answer is as Kim Togo said, and for question 1, i dont really get it, are trying to calculate the difference between the date selected and today? or the difference with 31/03/2012?
Ankur\m/ 22-Apr-11 8:23am    
Use 'Add Comment' below user's answer to discuss with him/her. He never gets notified if on add an answer.

A DateTimePicker class, you have the ability to custom format what informations you what to display.

DateTimePicker.CustomFormat[^]

If you known a start date and end date, use can you TimeSpan class to get total days.

DateTime start = DateTime.Now;
DateTime end = DateTime.Now.AddDays(25);

TimeSpan span = end - start;
int totalDays = span.TotalDays;



About question 2.

Take a look at DateTime.DaysInMonth[^]

You give it year and month and it will return total days in that year and month.
 
Share this answer
 
v2
Comments
Ankur\m/ 22-Apr-11 8:21am    
[moved from answer]
OP wrote:
Q 1. i have mentioned i am using only one calendar to calculate days upto 31st mar

Q 2. year and month may vary. so i have to calculate using datetimepicker tool to calculate days in month
1. Where do you need to display the days? In the DateTimePicker or in the textbox? If it is DateTimePicker, you are using the wrong control for this purpose. If you want to display it in the textbox, you the following:
int daysToGo;
if(yourDateTimePicker.Value.Month > 3){
daysToGo = (new DateTime(yourDateTimePicker.Value.Year + 1,3,31) - yourDateTimePicker.Value).Days;
}
else{
daysToGo = (new DateTime(yourDateTimePicker.Value.Year,3,31) - yourDateTimePicker.Value).Days;
}


2. This should help:

yourTextBox.Text = DateTime.DaysInMonth(yourDateTimePicker.Value.Year, yourDateTimePicker.Value.Month).ToString();
 
Share this answer
 
Comments
laurancelove 23-Apr-11 9:48am    
help me to solve the problem
yes friend now my problem is over.

i have another question in 1st query.

when i choose 11-apr-2011 it shows 354 days.but i need, it should consider

only be-forth date that is 10-apr-2011 so days = 355

2nd.

in my 2nd question if i am selecting the date like 12-may-2011 the

textbox should tells the remaining days= 20 because 11-may should take.

else days = 31
 
Share this answer
 

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