Date Calculation

G

Guest

IN VB.Net, you can use the DateDiff method to calculate the difference between 2 days. How do you do this in C#? I want to find the number of days between 2 dates. I there a 0 days, then I want to know the number of hours and minutes between 2 dates

I see there is a method for the DateTime object which allows me to subtract, but it requires a TimeSpan object, andI am not quite shure how to create this correctly. I presume it goes something like this..
daysLeft = _PM_Cutoff_Date.Subtract(new TimeSpan( ?, ?, ?, ?) where ? = int of Days, hours, minutes and seconds

Can someone clear up my confusion on how to do this calculation

Thanks a bunch!!
 
C

Cowboy \(Gregory A. Beamer\) [MVP]

DateTime dt = Convert.ToDateTime("1/1/2004");
DateTime dt1 = Convert.ToDateTime("3/1/2004");

TimeSpan ts = dt1.Subtract(dt);

Note, you can use compare to see which date is larger, if either could be
larger.

You also have this static method:

TimeSpan ts = System.DateTime.op_Subtraction(dt, dt1);


--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

************************************************
Think Outside the Box!
************************************************
Issac Gomez said:
IN VB.Net, you can use the DateDiff method to calculate the difference
between 2 days. How do you do this in C#? I want to find the number of
days between 2 dates. I there a 0 days, then I want to know the number of
hours and minutes between 2 dates.
I see there is a method for the DateTime object which allows me to
subtract, but it requires a TimeSpan object, andI am not quite shure how to
create this correctly. I presume it goes something like this...
daysLeft = _PM_Cutoff_Date.Subtract(new TimeSpan( ?, ?, ?, ?) where ? =
int of Days, hours, minutes and seconds.
 
M

mikeb

Issac said:
IN VB.Net, you can use the DateDiff method to calculate the difference between 2 days. How do you do this in C#? I want to find the number of days between 2 dates. I there a 0 days, then I want to know the number of hours and minutes between 2 dates.

I see there is a method for the DateTime object which allows me to subtract, but it requires a TimeSpan object, andI am not quite shure how to create this correctly. I presume it goes something like this...
daysLeft = _PM_Cutoff_Date.Subtract(new TimeSpan( ?, ?, ?, ?) where ? = int of Days, hours, minutes and seconds.

Can someone clear up my confusion on how to do this calculation?

You can also subtract 2 DateTimes to get a TimeSpan representing the
difference:

Dim daysLeft as TimeSpan

' remove any clock-time components from the cutoff date
' so subtracting DateTime.Today doesn't result in
' a 'fractional' TimeSpan

Dim normalizedCutoff as new DateTime( _PM_Cutoff_Date.Year, _
_PM_Cutoff_Date.Month, _
_PM_Cutoff_Date.Day)

daysLeft = normalizedCutoff.Subtract( DateTime.Today)

Console.WriteLine( "Days left: {0}", daysLeft.TotalDays)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top