Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Determine Number of Days Between Two Dates 1

Status
Not open for further replies.

JeaShe

Programmer
Mar 9, 2004
89
0
0
US
I'm trying to give a user the number of days beginning with the date the user provides up until today. I'll attach the code, but am really lost in trying to find the number of days in between the first month and this month. I'm sure there is a much cleaner way to do the other parts and am more than happy to consider one! I did perform a search, but no luck... Thanks in advance!

Code:
----------------------------
option explicit


Sub Main

Dim finalDay, rcvdYear as String, finalYear as String, numDyFirst
Dim getDy as String, numDy as double, daysPerPeriod , FnumMo, FnumYr, numDaylast
Dim getYr as String, numYr as Double, cNumYr , InDt as String, finalMonth as String
Dim getMo as String, numMo as double, todDate as String, rcvdMonth as String
Dim ckDy as Integer, totalDy as String, calendarfirstape as string, rcvdDay
Dim daysBetween as Double, numdays, msg1 as String

'WHAT IS THE RECEIVED DATE OF THE RETURN
InDt = InputBox("Enter return received date.")
InDt = Format(InDt, "mm/dd/yyyy")

'WHAT IS TODAY'S DATE
todDate =DateValue(Date$)
todDate = DateValue(Date$)

'PARSE RECEIVED AND TODAY'S (final) DATE
rcvdMonth = Format( Mid(InDt, 1, 2 ), "00")
finalMonth = Format(Mid(todDate, 1, 2 ), "00")

rcvdDay =Format( Mid(InDt, 4, 2 ), "00")
finalDay = Format(Mid(todDate, 4, 2), "00")

rcvdYear = Mid(InDt, 7, 4 )
finalYear = Format(Mid(todDate, 7, 4))

'DETERMINE HOW MANY DAYS ARE IN THE BEG DATE'S MONTH
Select Case rcvdMonth
Case "03", "05", "07", "08", "10", "12"
'TOTAL DYS THESE MONTHS
totalDy = "31"

Case "01"
'TOTAL DYS THIS MONTH
totalDy = "31"

Case "02"
'TOTAL DYS THIS MONTH
totalDy = "28"

Case "04", "06", "09", "11"
'TOTAL DYS THIS MONTH
totalDy = "30"

Case Else
msgbox "Problems with month selct cAsE."

End Select

'ADD NUMBER OF DAYS IN THE RCVD MONTH
numDyFirst = val(totalDy)- rcvdDay
'---------------------------------------------------------------------

'FIND NUMBER OF DAYS SO FAR THIS MONTH
Select Case finalMonth
Case "03", "05", "07", "08", "10", "12"
'TOTAL DYS THESE MONTHS
totalDy = "31"

Case "01"
'TOTAL DYS THIS MONTH
totalDy = "31"

Case "02"
'TOTAL DYS THIS MONTH
totalDy = "28"

Case "04", "06", "09", "11"
'TOTAL DYS THIS MONTH
totalDy = "30"

Case Else
msgbox "Problems with month selct cAsE."

End Select

'DETERMINE NUMBER OF DAYS FINAL MONTH
numDaylast = val(totalDy)- finalDay
'---------------------------------------------------------------------
Need help here!

'DETERMINE NUMBER OF DAYS FOR THE MONTHS IN BETWEEN
daysBetween =



'---------------------------------------------------------------------

numdays = numDyFirst + numDaylast + daysBetween

msg1 = "For the date you entered of: " & Chr$(13) & Chr$(10)
msg1 = msg1 & "you have " & numdays & " already."
msgbox msg1
End Sub
 




Hi,

Dates are NOT strings! Dates are NUMBERS. Today is 39559, meaning 39559 days since 12/31/1899.

Just calculate the DIFFERENCE, Date1 - Date2, to get the number of days


Skip,
[sub]
[glasses]Have you heard that the roundest knight at King Arthur's round table was...
Sir Cumference![tongue][/sub]
 
Ok... Don't I feel dumb?? Ok, well I did this and I'm still missing something. Any ideas??

Code
-------------------------------





Sub main
Dim str1 as String, nextweek, msgtext

str1=InputBox$("Enter the return received date:")
str1=CVDate(str1)

str2 = Date()
str2=CVDate(str2)


daysNum = CVar(str2) - CVar(str1)
msgbox daysnum


End Sub
 




Why do you insist on assigning dates to strings???
Code:
Sub main
    Dim str1 as String, nextweek, msgtext
    Dim dat1 as date, dat2 as date
    
    str1=InputBox$("Enter the return received date:")
    dat1=DateValue(str1)
    
    dat2= Date()
    [s]str2=DateValue(str2)[/s]
    
    
    daysNum = dat2-dat1
    msgbox daysnum
    
    
End Sub
When the user enters a "date" string in the input, it could be in any number of "date" string formats, some of which could yeild incorrect results.

It would be better to collect the YEAR, MONTH and DAY as separate values and then use the DateSerial function to convert to a Date Value.

Skip,
[sub]
[glasses]Have you heard that the roundest knight at King Arthur's round table was...
Sir Cumference![tongue][/sub]
 
I don't think there is a date data type in Extra... is there?
 


Code:
Sub main()
    Dim str1 As String
    Dim dat1, dat2, daysnum As Integer
    
    str1 = InputBox$("Enter the return received date:")
    dat1 = CVDate(str1)
    
    dat2 = CVDate(Date)
    daysnum = dat2 - dat1
    MsgBox daysnum
    
    
End Sub


Skip,
[sub]
[glasses]Have you heard that the roundest knight at King Arthur's round table was...
Sir Cumference![tongue][/sub]
 



FYI
HELP said:
The CVDate functions returns a variant of vartype 7 (date) that represents a date from January 1, 100 (-657434) through December 31, 9999 (2958465). A value of 2, for example, is returned if expression is ‘‘January 1, 1900’‘.


Copyright 1996 - 1999, Attachmate Corporation. All Rights Reserved.

Skip,
[sub]
[glasses]Have you heard that the roundest knight at King Arthur's round table was...
Sir Cumference![tongue][/sub]
 
Nope really nasty string date in Extra.
Code:
Sub main

    Dim s_RtRcvdDt as String
    Dim i_DaysDiff as Integer

    s_RtRcvdDt = InputBox$("Enter the return received date:")

    if IsDate(s_RtRcvdDt) then
        i_DaysDiff = DateValue(Date()) - DateValue(s_RtRcvdDt)
        msgbox i_DaysDiff    
    Else
        MsgBox "Invalid date format entered."
    End If
    
End Sub

[small]Sometimes you gotta leave your zone of safety. You have to manufacture Inspirado. You gotta get out of the apartment. You've got to run with the wolves. You've got to dive into the ocean and fight with the sharks. Or just treat yourself to a delicious hot fudge sundae........ with nuts. - Jack Black[/small]
 
Bah, I should know better than to mix work with fun. Look how long my browser was open while I was wasting time working. :)

[small]Sometimes you gotta leave your zone of safety. You have to manufacture Inspirado. You gotta get out of the apartment. You've got to run with the wolves. You've got to dive into the ocean and fight with the sharks. Or just treat yourself to a delicious hot fudge sundae........ with nuts. - Jack Black[/small]
 



I do most of my work in Excel VBA, so I do get things confused.

BUT...

there is a DateValue function in Extra.

there is a DateSerial function in Extra.

You cannot do arithmetic with strings.

Using date strings like mm/dd/yyy or dd/mm/yyyy or yy/mm/dd, you will not get correct collating, in SORT operations, less than or greater than operations.

Skip,
[sub]
[glasses]Have you heard that the roundest knight at King Arthur's round table was...
Sir Cumference![tongue][/sub]
 
And it took me 3 minutes to read the posts from 21 Apr 08 16:44 down to mine, must be a Mon.

[small]Sometimes you gotta leave your zone of safety. You have to manufacture Inspirado. You gotta get out of the apartment. You've got to run with the wolves. You've got to dive into the ocean and fight with the sharks. Or just treat yourself to a delicious hot fudge sundae........ with nuts. - Jack Black[/small]
 
Right as usual Skip, I wouldn't have followed your working example had I refreshed my browser and actually seen it prior to posting. Your a hard act for me to follow.

At least my belated post contributed some value with the IsDate() function reference.

[small]Sometimes you gotta leave your zone of safety. You have to manufacture Inspirado. You gotta get out of the apartment. You've got to run with the wolves. You've got to dive into the ocean and fight with the sharks. Or just treat yourself to a delicious hot fudge sundae........ with nuts. - Jack Black[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top