Click here to Skip to main content
15,884,425 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
I have two textboxes in which I am storing dates from calender. I have a third textbox in which I want to store the number of days after calculating the difference between the two dates in c#.
How can I do that? Plz help.

Thank you.
Posted
Updated 9-Jul-17 3:24am
v2
Comments
[no name] 3-Sep-12 8:43am    
You subtract one from the other and then display the result in the third. How else do you think?

Any effort so far? Google[^] has hell lot of topics on this.

Here are few of those:

http://www.dotnetspider.com/resources/458-How-find-difference-between-two-Dates-C-or.aspx[^]

http://msdn.microsoft.com/en-us/library/8ysw4sby.aspx[^]

Basically, you need to make use of TimeSpan[^].
 
Share this answer
 
Comments
Mohamed Mitwalli 3-Sep-12 9:22am    
5+
Manas Bhardwaj 3-Sep-12 9:23am    
thx!
This can be easily accomplished using an object of Type "TimeSpan". For example: let's assume that we want to know the number of days between the max. and min. values for the DateTime Type and show it in a Console window, then I may write something like:
C#
DateTime d1=DateTime.MinValue;
DateTime d2=DateTime.MaxValue;
TimeSpan span=d2-d1;
Console.WriteLine( "There're {0} days between {1} and {2}" , span.TotalDays, d1.ToString(), d2.ToString() );
 
Share this answer
 
v2
Let's say that your DateTimes are called start and end.
DateTime start = new DateTime(2009, 6, 14);
DateTime end = new DateTime(2009, 12, 14);

We have established our DateTimes to June 14, 2009 and December 14, 2009.

Now, let's find the difference between the two. To do this, we create a TimeSpan:
TimeSpan difference = end - start;

With this TimeSpan object, you can express the difference in times in many different ways. However, you specifically asked for the difference in days, so here's how you can get that:
Console.WriteLine("Difference in days: " + difference.Days);

Thus, the property is called TimeSpan.Days.
 
Share this answer
 
try this:

IFormatProvider theCultureInfo = new System.Globalization.CultureInfo("en-GB", true);
DateTime from_date = DateTime.ParseExact(txt_from_date.Text, "dd/MM/yyyy", theCultureInfo);
DateTime to = DateTime.ParseExact(txt_to_date.Text, "dd/MM/yyyy", theCultureInfo);
txt_total_days.Text = ((to - from_date).TotalDays).ToString();


if u any problm, please post it.
 
Share this answer
 
v2
Comments
Mohamed Mitwalli 3-Sep-12 9:32am    
Hi Mits
DateTime.Compare it's for comparison between two dates , and it will return 1 or -1 , if the first date earlier than second one will return -1
and vice versa
[no name] 4-Sep-12 0:43am    
Thanks, my mistake .. SOrry 4 thattt and i update the My answer....
miss angel plz try google before You post question i give two links

[^]

and

http://msmvps.com/blogs/gaidar/archive/2005/09/06/65514.aspx[^]
You can find more also
 
Share this answer
 
Comments
Member 11036620 27-Aug-14 7:34am    
i want difference between 2 calendar in c# program and to store in label
consider Dates in both text boxes , now i have to get number of days when i subtracted from one another.

DateTime dt1=Convert.ToDateTime(Textbox1.text);
DateTime dt1=Convert.ToDateTime(Textbox1.text);
TimeSpan ts=dt.subtract(dt1);
int NoOfDays=dt.Days;
textbox3.text=NoOfDays;
 
Share this answer
 
May be It will help you.

C#
DateTime dt1=Convert.ToDateTime(TextBox1.Text);
DateTime dt2=Convert.ToDateTime(TextBox2.Text);
TextBox3.Text=""+((dt1>dt2)?(dt1-dt2).TotalDays:(dt2-dt1).TotalDays);


Happy Coding
 
Share this answer
 
C#
private void button1_Click(object sender, EventArgs e)
        {
            TimeSpan m1stDate = dTP1.Value- dTP2.Value;
            textBox1.Text = m1stDate.ToString();
        }
 
Share this answer
 
Comments
CHill60 23-Sep-13 15:03pm    
Unfortunately this doesn't even compile ... unless of course you change the OPs textboxes into datetimepickers - which is off topic. It was also effectively resolved a year ago.

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