How to find the number of days between two dates

raj3

New member
Joined
Jun 29, 2007
Messages
3
Programming Experience
3-5
Hi,

I need to write a program that will find the number of days between two dates. I cannot use the inbuilt date functions or the DateTime Type and this program should work with any date between 1900 and 3000.

This is to find out how best we can use OO skils to write this program

hope some one can help me overcome this problem
 
Thanks neal

But the requirement is

I shouldn't use the inbuilt date functions or the DateTime Type and this program should work with any date between 1900 and 3000.

This is to find out how best we can use OO skils to write this program
 
Hi neal

This may sounds like a easy solution to many here but actually it is very importent and tough for me. I know we can do this easily using sql statements but this is a case of how best we do by implementing all OO concepts. That is the reason it clearly says we shouldn't use built date functions or the DateTime Type

i really appreciate every one for any more thoughts on this
 
The date object has built in properties such as Day, Month, Year if you write a function that accepts two date objects in .NET you can easily create your own date diff function. This might work.
 
VB.NET:
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim d1 As New Date(1999, 1, 4)
        Dim d2 As New Date(3000, 2, 22)
        Console.WriteLine("There are " & dateDifference(d1, d2) & " days between " & d1.ToString & " & " & d2.ToString)

    End Sub
    Private Function dateDifference(ByVal d1 As Date, ByVal d2 As Date) As Integer
        Dim intReturn As Integer = 0
        Dim dt1 As Date = d1
        Dim dt2 As Date = d2

        If dt1 = dt2 Then
            Return 0
        ElseIf dt1 > dt2 Then
            Do Until dt1 = dt2
                dt2 = dt2.AddDays(1)
                intReturn += 1
            Loop
        ElseIf dt1 < dt2 Then
            Do Until dt1 = dt2
                dt1 = dt1.AddDays(1)
                intReturn += 1
            Loop
        End If


        Return intReturn

    End Function
 
You need to create a julian date class with two methods. one to convert a date to julian date and another to convert back. You can then add or subtract the julian dates to get number of days between the dates. Google for julian date code.
 
Back
Top