Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Subtracting Dates

Status
Not open for further replies.

tmryan

Programmer
Dec 22, 2000
73
0
0
US
New to C#, and I'm looking for a way to find the number of days between 2 dates. I've created 2 dateTimePickers (short format) on a form. I want to calculate the number of days between them. In VB I could do it with "lblResult.Caption = Abs(DateDiff("d", dtpStartDate, dtpendDate)) & " days"". I can't find something similar to DateDiff.

Thanks
Tim

Tim Ryan
PROGRESS Developer
 
You're going to kick youself, and then begin to like C# even more.

You use the subtraction operator:
Code:
TimeSpan ts = dtpStartDate - dtpendDate;
lblResult.Caption = ts.Days.ToString() & " days";
In theory, you could do something like this, but I don't have my IDE up at the moment to test for you:
Code:
lblResult.Caption = (dtpStartDate - dtpendDate).Days.ToString() & " days";
Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
Thanks chiph - that did the trick.

Tim Ryan
PROGRESS Developer
 
Did the second form work?

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top