473,408 Members | 2,734 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,408 software developers and data experts.

Calculating date differences in C# (DateDiff for c#??)

Hi
I want to calculate the difference between 2 dates in C#.

I know there is a function in VB, called DateDiff, but I don't want to ref
the VB library, and want to try to do it natively in C#.

Is there a similar function available in the C# or standard .NET libraries?
Thanks
Nov 16 '05 #1
2 15000
Look up the Subtraction operator of the DateTime and TimeSpan structures.
You can get the difference between combinations of DateTimes, TimeSpans
returned as either DateTimes or TimeSpans

HTH

DalePres
MCAD, MCDBA, MCSE

"Paul Aspinall" <pa**@aspy.co.uk> wrote in message
news:%_********************@fe3.news.blueyonder.co .uk...
Hi
I want to calculate the difference between 2 dates in C#.

I know there is a function in VB, called DateDiff, but I don't want to ref
the VB library, and want to try to do it natively in C#.

Is there a similar function available in the C# or standard .NET
libraries?
Thanks

Nov 16 '05 #2
Look at System.DateTime and System.TimeSpan.

http://msdn.microsoft.com/library/en...classtopic.asp
http://msdn.microsoft.com/library/en...classtopic.asp

DateTime dtThen = new DateTime (2004, 1, 1);
DateTime dtNow = DateTime.Now;
TimeSpan ts = dtNow - dtThen;

DateTime also contains functions like AddHours & AddMinutes. I don't think
there is any direct equivalent to DateDiff, but you can certainly accomplish
the same thing in other ways using DateTime and TimeSpan.

- Jon

"Paul Aspinall" <pa**@aspy.co.uk> wrote in message
news:%_********************@fe3.news.blueyonder.co .uk...
Hi
I want to calculate the difference between 2 dates in C#.

I know there is a function in VB, called DateDiff, but I don't want to ref
the VB library, and want to try to do it natively in C#.

Is there a similar function available in the C# or standard .NET
libraries?
Thanks

Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: androtech | last post by:
Hello, I'm looking for a function that returns a date range for a specified week number of the year. I'm not able to find functions like this anywhere. Any pointers/help would be much...
4
by: Lynn | last post by:
On a form I have Date_Start Date_End I have a new Date_Start1 Date_End1 which the use inputs. I need to validate that Date_Start1 and...
2
by: Shaun | last post by:
Hi, I have a table called Bookings which has two important columns; Booking_Start_Time and Booking_End_Time. These columns are both of type DATETIME. Given any day how can I calculate how many...
2
by: Richard Gomez | last post by:
I would like to have a persons age in my database using their date of birth. Can anybody give me some sugestios how i can do this
0
by: UBI | last post by:
Vorrei suggerire una funzione completa per la differenza fra due date. (questo č codice freeware, niente di illegale) La funzione seguente permette il calcolo di una differenza precisa tra due...
1
by: pauly | last post by:
Hello All, I have been trying to create a query that extracts data from a table and calculates the elapsed time between records. The table called "Imported_table". I need to be able to calculate...
32
by: whiteboy | last post by:
What's up Everyone? I really need some help. I've been pulling the hair out of my head trying to research how to calculate a late date. Here is what I'm trying to do: DATE VOUCHER...
4
by: scott | last post by:
Hi! I have a table with a StartTime and EndTime and want to calculate number of hours worked. Is it possible to get a "Number" answer? E.g. 9:00am (StartTime) worked to 4:00pm (EndTime) = 7...
8
by: =?Utf-8?B?QWw=?= | last post by:
I am working in vb2005. how can I calculate business days (not including holidays and weekends) between 2 dates? thanks Al
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.