Date difference in C#

G

Guest

Hi,

What is the synatx in C# for calculating date difference between 2 dates in terms of no. of days?

Thanks,
Ben
 
B

Brian W

Better yet, instead of using a VB function and since you're using C# try
this:

DateTime dt1 = <some date>;
DateTime dt2 = <some other date>;

TimeSpan ts = dt1 - dt2;

int days = ts.Days;


HTH
Brian W
 
C

Cowboy \(Gregory A. Beamer\)

Why would someone use this for C#? Why not use the Compare method of the
DateTime structure?

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

**********************************************************************
Think Outside the Box!
**********************************************************************
 
C

Cowboy \(Gregory A. Beamer\)

Look up "DateTime.Subtract Method". You subtract one from another and get a
time span object:

TimeSpan ts = date1.Subtract(date2); // both are DateTime

NOTE: The suggestion of Compare() in answer to DateDiff (another answer
post) was a bit tongue in cheek. ;->

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

**********************************************************************
Think Outside the Box!
**********************************************************************
 
L

Laszlo Szijarto

DateTime dt1 = new DateTime(....);
DateTime dt2 = new DateTime(...);

TimeSpan ts = dt1 - dt2;

or use

TimeSpan = DateTime.Now - dt1;

or whatever
 
M

Michael

I stand corrected with a big "Whoops!"
--a vb.net programmer who thought they had those same things over in the C#
world.
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top