473,386 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Date difference in C#

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
7 10993
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
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
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
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
Ben
Thanks all.
Nov 18 '05 #6
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Richard Hollenbeck | last post by:
I'm trying to write some code that will convert any of the most popular standard date formats twice in to something like "dd Mmm yyyy" (i.e. 08 Jan 1908) and compare the first with the second and...
5
by: tamilan71 | last post by:
Hello All I have table with following fields: GroupId VisitDate 1 10/19/1993 1 11/24/1998 2 10/18/1993 2 10/29/1998 3 ...
7
by: Edward Mitchell | last post by:
I have a number of DateTimePicker controls, some set to dates, some set to a format of Time. The controls are all embedded in dialogs. I created the controls by dragging the DateTime picker from...
16
by: Atley | last post by:
I am trying to get a overall difference on two dates, I can get the difference in Years, Months, Weeks, Days, Hours, Minutes, Seconds, no problems... What I cannot seem to figure out is how to...
6
by: Scott | last post by:
I have a start date and an end date in my project that is defaulted to the current date at run-time using the following code... Date1.Value = Now() Date2.Value = Now() I then have some code...
13
by: priyasmita_guha | last post by:
Here is a program- /* PROGRAM: To find the difference between two dates */ #include<dos.h> #include<stdio.h> #include<conio.h> #include<process.h> void valid_date(int,int,int); int...
4
by: jamesyreid | last post by:
Hi, I'm really sorry to post this as I know it must have been asked countless times before, but I can't find an answer anywhere. Does anyone have a snippet of JavaScript code I could borrow...
3
by: bbawa1 | last post by:
Hi, I have a table which has a field ItemsReceived of type datetime. I have a grid view which has two columns. In first column i have to show the data from field ItemsReceived and in second...
7
by: rdawadiuk | last post by:
hi, I would like to calculate the date difference between two dates using field names which I have in my table. The field name is " Account opened on" for eg If the "Account open on" is Jan 5,...
6
by: krishnakant Mane | last post by:
hello, I am strangely confused with a date calculation problem. the point is that I want to calculate difference in two dates in days. there are two aspects to this problem. firstly, I can't get...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.