By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,190 Members | 1,412 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,190 IT Pros & Developers. It's quick & easy.

Date difference in C#

P: n/a
Ben
Hi,

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

Thanks,
Ben
Nov 18 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Check out the DateDiff Function
http://msdn.microsoft.com/library/de...ctdatediff.asp

"Ben" <bl*********@yahoo.com> wrote in message
news:D0**********************************@microsof t.com...
Hi,

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

Nov 18 '05 #2

P: n/a
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!
************************************************** ********************
"Michael" <raterus@localhost> wrote in message
news:%2***************@TK2MSFTNGP10.phx.gbl...
Check out the DateDiff Function
http://msdn.microsoft.com/library/de...ctdatediff.asp
"Ben" <bl*********@yahoo.com> wrote in message
news:D0**********************************@microsof t.com...
Hi,

What is the synatx in C# for calculating date difference between 2 dates

in terms of no. of days?

Thanks,
Ben


Nov 18 '05 #3

P: n/a
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

"Ben" <bl*********@yahoo.com> wrote in message
news:D0**********************************@microsof t.com...
Hi,

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

Nov 18 '05 #4

P: n/a
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!
************************************************** ********************
"Ben" <bl*********@yahoo.com> wrote in message
news:D0**********************************@microsof t.com...
Hi,

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

Nov 18 '05 #5

P: n/a
Ben
Thanks all.
Nov 18 '05 #6

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

"Cowboy (Gregory A. Beamer)" <No************@comcast.netNoSpamM> wrote in
message news:ud**************@TK2MSFTNGP10.phx.gbl...
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!
************************************************** ********************
"Michael" <raterus@localhost> wrote in message
news:%2***************@TK2MSFTNGP10.phx.gbl...
Check out the DateDiff Function

http://msdn.microsoft.com/library/de...ctdatediff.asp

"Ben" <bl*********@yahoo.com> wrote in message
news:D0**********************************@microsof t.com...
Hi,

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

Thanks,
Ben



Nov 18 '05 #7

P: n/a
DateTime dt1 = new DateTime(....);
DateTime dt2 = new DateTime(...);

TimeSpan ts = dt1 - dt2;

or use

TimeSpan = DateTime.Now - dt1;

or whatever
Nov 18 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.