Click here to Skip to main content
16,018,417 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I have a service commencement date and select date.Here user will select the date from date time picker.If selected date is greater than service comencement date then it will display the month number e.g if service commencement date =10/10/2011 and selected date=01/12/2011 then it will show 2 in textbox or if selected date=9/9/2011 then it will display -1 in textbox. Here is my code
C#
DateTime dService= frmVwPersuit.dServiceCommDate;
           DateTime date = this.dateTimePicker1.Value;

           if (DateTime.Compare(dService, date) > 0)
           {
               this.txtMnth.Text = date.ToString("MM");
           }
           else
           {
               this.txtMnth.Text = date.ToString("-MM");
           }
Posted
Updated 4-Dec-11 23:25pm
v2
Comments
[no name] 5-Dec-11 5:26am    
edited ;)

C#
DateTime dService= frmVwPersuit.dServiceCommDate;
DateTime date = this.dateTimePicker1.Value;

this.txtMnth.Text = DateTime.Compare(dService, date) > 0 : date.Month.ToString() ? string.Format("-{0}",date.Month.ToString());


hope this will work.

thanks
-amit
 
Share this answer
 
C#
int difference = date.Month - dService.Month;
this.txtMnth.Text = String.Format("{0:D2}", difference);
 
Share this answer
 
Comments
Wonde Tadesse 5-Dec-11 12:22pm    
5+
try this..

C#
DateTime dService= frmVwPersuit.dServiceCommDate;
            DateTime date = this.dateTimePicker1.Value;
 
            if (DateTime.Compare(dService, date) > 0)
            {
                this.txtMnth.Text = date.Month.ToString();
            }
            else
            {
                this.txtMnth.Text = date.Month.ToString();

            }
 
Share this answer
 
Comments
Wonde Tadesse 5-Dec-11 12:22pm    
5+
Richard MacCutchan 5-Dec-11 13:09pm    
This is the same as OP's question and merely returns the month number in both cases.

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