Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Interval (in days) between 2 dates

Posted on 2002-05-26
7
Medium Priority
?
160 Views
Last Modified: 2010-05-02
Hello,

How can I find the interval between 2 dates?

Examples:
IntervalDates("16/4/2002", "24/4/2002") will return 8
IntervalDates("28/2/2002", "1/3/2002") will return 1
IntervalDates("28/2/2000", "1/3/2000") will return 2
IntervalDates("31/12/2001", "1/1/2002") will return 1
IntervalDates("26/5/2002", Date)
etc...

Thanks in advance.
0
Comment
Question by:slavikn
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 70

Accepted Solution

by:
Éric Moreau earned 300 total points
ID: 7035653
Use DateDiff
0
 
LVL 44

Expert Comment

by:bruintje
ID: 7035660
Hi slavikn,

you can try something like

function intervaldate(Byval Date1 as string, _
                      Byval Date2 as string, _)
  intervaldate = cdate(date1)  -  cdate(date2)
end function

:O)Bruintje
0
 
LVL 4

Expert Comment

by:Monchanger
ID: 7035668
Just subtract one date from the other.

Public Function IntervalDates(ByVal StartDate As Date, ByVal EndDate As Date) As Integer

    IntervalDates = EndDate - StartDate
End Function

(PS - if you want to compare dates that are very far away, you should change the return value to 'long')

slavikn, you seem to be quite new to VB. I suggest that instead of spending a lot of points on this site with these questions (not that I mind collecting them ;-) ), you just check out a few of these sites for tutorials and code examples:

This site contains a lot of code by all types of programmers (Including a lot of beginner stuff)
http://www.planet-source-code.com

This site has a lot of well organized code:
http://www.freevbcode.com/

Another great site:
http://www.vbsquare.com/

Good luck.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 70

Expert Comment

by:Éric Moreau
ID: 7035669
I just saw that some of your dates are inversed, you should use the mm/dd/yyyy format not to have problem.

Private Sub Form_Load()
    MsgBox IntervalDates("4/16/2002", "4/24/2002") 'will return 8
    MsgBox IntervalDates("2/28/2002", "3/1/2002") 'will return 1
    MsgBox IntervalDates("2/28/2000", "3/1/2000") 'will return 2
    MsgBox IntervalDates("12/31/2001", "1/1/2002") 'will return 1
    MsgBox IntervalDates("5/26/2002", Date)
End Sub

Private Function IntervalDates(ByVal pdtm1 As Date, ByVal pdtm2 As Date) As Integer
    IntervalDates = Abs(DateDiff("d", pdtm1, pdtm2))
End Function
0
 
LVL 2

Expert Comment

by:Crin
ID: 7036406
emoreau, world is not limited to USA/Canada and somewhere else people uses dd/mm/yyyy date format, CDate function as well as other conversion functions are based on the system local settings. Only dates where day is 13 or greater will be converted right way in any conditions.

Sincerely,
Crin
0
 

Expert Comment

by:mani_m_in
ID: 7036508
      You can find the intervals between two Dates  by using the DateDiff Function.

       The syntax for this function is

       DateDiff("d",FirstDate,SecondDate)

 -it returns the interval between the two dates.

the same function also used for finding month and year difference between two dates by chaning the first parameter.

MM -- for Month difference
YYYY-Year Difference
 
0
 
LVL 1

Author Comment

by:slavikn
ID: 7037663
Thanks!
Most of all other answeres used the same function.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

718 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question