# How to calculate number of days between 2 Dates/time

Page 1 of 1

## 3 Replies - 44772 Views - Last Post: 15 February 2009 - 02:00 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=86090&amp;s=a1821e50e8eccb056182b7af139e3818&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 AXRabbit

Reputation: 0
• Posts: 9
• Joined: 04-February 09

# How to calculate number of days between 2 Dates/time

Posted 11 February 2009 - 04:15 AM

Hi guys, I am currently doing a Car rental website project. Like my Car collection and return date is like in the website. http://www.avis.com/...visHome/home.ac

Well i need a function or method to calculate the number of days between Collection date/time and return date/time.

Time spent in reading my problem is deeply appreciated. Hope that anyone who know can help.

Thanks~
-Kelvin

Is This A Good Question/Topic? 0

## Replies To: How to calculate number of days between 2 Dates/time

### #2 Nikhil_07n

• The cheese stands alone..

Reputation: 54
• Posts: 2,490
• Joined: 09-January 09

## Re: How to calculate number of days between 2 Dates/time

Posted 11 February 2009 - 12:56 PM

```Private Sub Datecalc_Click()

Dim Date1 as Date
Dim Date2 as Date

Date1=Inputbox("Enter date1")
Date2=Inputbox("Enter date2")

Print DateDiff("d",Date1,Date2)

'Here "d" is used to set interval to days

End Sub

```

Simple.

Say Thanks!

For more date functions, check my tutorial out!

http://www.dreaminco...wtopic83102.htm

### #3 AXRabbit

Reputation: 0
• Posts: 9
• Joined: 04-February 09

## Re: How to calculate number of days between 2 Dates/time

Posted 15 February 2009 - 01:58 AM

Hi, I tried :

Dim startdate As DateTime
Dim enddate As DateTime

'Section 1
startdate = MonthList1.SelectedItem.Text & "/" & DayList1.SelectedItem.Text & "/" & YearList1.SelectedItem.Text & " " & HourList1.SelectedItem.Text & ":" & MinuteList1.SelectedItem.Text
enddate = MonthList2.SelectedItem.Text & "/" & Daylist2.SelectedItem.Text & "/" & YearList2.SelectedItem.Text & " " & HourList2.SelectedItem.Text & ":" & MinuteList2.SelectedItem.Text

'Section 2
DateDiff(DateInterval.DayOfYear, startdate, enddate)

But i cant get the answer.

and also..
'
I also tried to over write section 2 for trial and error purposes.

'Section 2
Dim d as integer
DateDiff("d",Date1,Date2)
d= label.text

But failed.
I also tried

DateDiff(DateInterval.Day, startdate, enddate)

Fail again.

Dayinterval.Day and Dayinterval.Dayofyear has a value of 3 and 4 respectively. How come?

This post has been edited by AXRabbit: 15 February 2009 - 10:59 AM

### #4 Nikhil_07n

• The cheese stands alone..

Reputation: 54
• Posts: 2,490
• Joined: 09-January 09

## Re: How to calculate number of days between 2 Dates/time

Posted 15 February 2009 - 02:00 PM

I dint get exactly what you're tryin to say but i've got a few suggestions for ya.

1) I think you are misjudging the function

let the output textbox be 'txtResult'

```txtResult.Text = DateDiff("d", startdate, enddate)

```

2) Actually, what you are doing in Section2 is WRONG!

You are assigning a value(label.text), to a declared variable 'd', which is USELESS!

the "d" used in datediff function has NO relation with this 'd'(to which you are assigning value of label.text).

USE THIS CODE(well its the old one only but this time i've used YOUR variables) :

```Dim startdate As DateTime
Dim enddate As DateTime

startdate = MonthList1.SelectedItem.Text & "/" & DayList1.SelectedItem.Text & "/" & YearList1.SelectedItem.Text & " " & HourList1.SelectedItem.Text & ":" & MinuteList1.SelectedItem.Text

enddate = MonthList2.SelectedItem.Text & "/" & Daylist2.SelectedItem.Text & "/" & YearList2.SelectedItem.Text & " " & HourList2.SelectedItem.Text & ":" & MinuteList2.SelectedItem.Text

'till here we are correct

txtResult.text= "The difference(in days) between the two dates is " & DateDiff("d",startdate, enddate)

'We did it, we did it!!

```

If you still have a prob. attach the program and i'll correct it for you.
But PLEASE, try out the above things first!