We help IT Professionals succeed at work.

How do i subtract date1 from date2 and show the results

bmanmike39
bmanmike39 asked
on
Medium Priority
518 Views
Last Modified: 2013-12-16
How do i subtract date1 from date2, and display the results.

Code i have:
DateTime date1 = DateTime.MinValue;
DateTime date2 = DateTime.MinValue;

DateTime.TryParse(x2, out date1);
DateTime.TryParse(x3, out date2);

Open in new window

Comment
Watch Question

Top Expert 2011

Commented:
Your first two lines look funky... where are you getting "DateTime.MinValue"?

Anyway.... try this

Dim date1 As DateTime = Convert.ToDateTime(date one goes here)
Dim date2 As DateTime = Convert.ToDateTime(date two goes here)
Dim ts As TimeSpan = date2.Subtract(date1)

Response.Write(ts.Days)

Open in new window

HainKurtSr. System Analyst
CERTIFIED EXPERT

Commented:
what about this

DateTime dateDiff = DateTime.MinValue;
DateTime.TryParse(date2-date1, out dateDiff );
Top Expert 2011
Commented:
Or this:
TimeSpan ts=new TimeSpan();
ts=date1-date2;

Open in new window


Several ways depending on whether you want to use DateDiff or TimeSpan and how you like your syntax to look.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.