Qbasicnews.com
September 25, 2020, 07:40:47 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Back to Qbasicnews.com | QB Online Help | FAQ | Chat | All Basic Code | QB Knowledge Base
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: Days between two dates  (Read 2659 times)
StarOnStarOff
New Member

Posts: 2


« on: April 29, 2004, 08:11:44 PM »

Hello all,

My assignment is to test out three function: ShortDate & LongDate take user input (in the form of MM-DD-YYYY) and print out short/long dates like: October 30, 2004. I've got all that fine and dandy, but the third function I'm supposed to do is find the difference between two dates - one is user inputted, the other is the one from the compueter DATE$. I have no clue how to go about it.

Here's my program so far. Its kinda long, so please don't be scared :p Its really really simple up to the point where I'm stuck, which is allll the way at the bottom where I've got function "DateDiff"
Code:
DECLARE FUNCTION DateDiff& (strDate1 AS STRING, strDate2 AS STRING)
DECLARE FUNCTION LongDate$ (strDate AS STRING)
DECLARE FUNCTION ShortDate$ (strDate AS STRING)
DECLARE FUNCTION GetTextualDate$ (strDate AS STRING, intMode AS INTEGER)
DECLARE FUNCTION CheckDateFormat% (strDate AS STRING)

''''''''''''''''''''''''''''''''''''''''''
' DECLARATION
''''''''''''''''''''''''''''''''''''''''''
DIM strDate1 AS STRING
DIM strDate2 AS STRING
DIM strShortDate AS STRING
DIM strLongDate AS STRING
DIM lngDateDiff AS LONG

''''''''''''''''''''''''''''''''''''''''''
' INPUT / ASSIGNMENT
''''''''''''''''''''''''''''''''''''''''''
COLOR 15, 1
CLS

PRINT "Enter a date in the format of MM-DD-YYYY"
DO
    INPUT ">", strDate1
    IF CheckDateFormat%(strDate1) = 1 THEN
        EXIT DO
    ELSE
        PRINT "Incorrect date format. Try again."
    END IF
LOOP

strDate2 = DATE$
strShortDate = ShortDate$(strDate1)
strLongDate = LongDate$(strDate1)
lngDateDiff = DateDiff&(strDate1, strDate2)

''''''''''''''''''''''''''''''''''''''''''
' OUTPUT
''''''''''''''''''''''''''''''''''''''''''
CLS
PRINT TAB(34); "DATE STUFF"
PRINT TAB(34); "----------"
PRINT
PRINT "Date #1 (yours):"; TAB(40); strDate1
PRINT "Date #2 (computers):"; TAB(40); strDate2
PRINT "Short Date:"; TAB(40); strShortDate
PRINT "Long Date:"; TAB(40); strLongDate
PRINT "Date Difference (between #1 and #2):"; TAB(40); lngDateDiff; " Days"

END

''''''''''''''''''''''''''''''''''''''''''
' CheckDateFormat
'  - Checks user input to ensure it is
'    in the correct format (MM-DD-YYYY)
''''''''''''''''''''''''''''''''''''''''''
FUNCTION CheckDateFormat% (strDate AS STRING)
    DIM strMonth AS STRING
    DIM strDay AS STRING

    strMonth = MID$(strDate, 1, 2)
    strDay = MID$(strDate, 4, 2)

    IF LEN(strDate) <> 10 THEN
        CheckDateFormat = 0
        PRINT "Wrong length"
    ELSEIF MID$(strDate, 3, 1) <> "-" THEN
        CheckDateFormat = 0
        PRINT "Hyphen after month missing"
    ELSEIF MID$(strDate, 6, 1) <> "-" THEN
        CheckDateFormat = 0
        PRINT "Hyphen after day missing"
    ELSEIF VAL(strMonth) < 0 OR VAL(strMonth) > 12 THEN
        CheckDateFormat = 0
        PRINT "Month range incorrect"
    ELSEIF VAL(strDay) < 0 OR VAL(strMonth) > 31 THEN
        CheckDateFormat = 0
        PRINT "Day range incorrect"
    ELSE
        CheckDateFormat = 1
    END IF
END FUNCTION

''''''''''''''''''''''''''''''''''''''''''
' GetTextualDate
'  - Returns a textual date, long or short
'    depending on intMode (1=short, 2=long)
''''''''''''''''''''''''''''''''''''''''''
FUNCTION GetTextualDate$ (strDate AS STRING, intMode AS INTEGER)

    '''''''''''''''
    ' DECLARATION
    '''''''''''''''
    DIM strMonth AS STRING
    DIM strDay AS STRING
    DIM strYear AS STRING
   
    '''''''''''''
    ' ASSIGNMENT
    '''''''''''''
    ' If invalid mode, assume mode 1
    IF intMode < 0 OR intMode > 2 THEN
        intMode = 1
    END IF

    strMonth = LEFT$(strDate, 2)
    strDay = MID$(strDate, 4, 2)
    strYear = RIGHT$(strDate, 4)

    ''''''''''
    ' PROCESS
    ''''''''''
    SELECT CASE VAL(strMonth)
        CASE 1
            strMonth = "January"
        CASE 2
            strMonth = "February"
        CASE 3
            strMonth = "March"
        CASE 4
            strMonth = "April"
        CASE 5
            strMonth = "May"
        CASE 6
            strMonth = "June"
        CASE 7
            strMonth = "July"
        CASE 8
            strMonth = "August"
        CASE 9
            strMonth = "September"
        CASE 10
            strMonth = "October"
        CASE 11
            strMonth = "November"
        CASE 12
            strMonth = "December"
    END SELECT

    IF intMode = 1 THEN
        strMonth = MID$(strMonth, 1, 3) + "."
    END IF

    GetTextualDate = strMonth + " " + strDay + ", " + strYear
END FUNCTION

''''''''''''''''''''''''''''''''''''''''''
' LongDate
'  - Alias for: GetTextualDate(strDate, 2)
''''''''''''''''''''''''''''''''''''''''''
FUNCTION LongDate$ (strDate AS STRING)
    LongDate = GetTextualDate$(strDate, 2)
END FUNCTION

''''''''''''''''''''''''''''''''''''''''''
' ShortDate
'  - Alias for: GetTextualDate(strDate, 1)
''''''''''''''''''''''''''''''''''''''''''
FUNCTION ShortDate$ (strDate AS STRING)
    ShortDate = GetTextualDate$(strDate, 1)
END FUNCTION

''''''''''''''''''''''''''''''''''''''''''
' DateDiff
'  - Find difference (days) between two
'    dates
''''''''''''''''''''''''''''''''''''''''''
FUNCTION DateDiff& (strDate1 AS STRING, strDate2 AS STRING)
   
    ''''''''''''''
    ' DECLARATION
    ''''''''''''''
    DIM intMonth1 AS INTEGER
    DIM intMonth2 AS INTEGER
    DIM intDay1 AS INTEGER
    DIM intDay2 AS INTEGER
    DIM intYear1 AS INTEGER
    DIM intYear2 AS INTEGER

    DIM intDaysDiff AS INTEGER
    DIM intDayDiff AS INTEGER
    DIM intMonthDiff AS INTEGER
    DIM intYearDiff AS INTEGER

    ''''''''''''''''''''
    ' PROCESSING / CALC
    ''''''''''''''''''''
    intMonth1 = VAL(LEFT$(strDate1, 2))
    intMonth2 = VAL(LEFT$(strDate2, 2))
    intDay1 = VAL(MID$(strDate1, 4, 2))
    intDay2 = VAL(MID$(strDate2, 4, 2))
    intYear1 = VAL(RIGHT$(strDate1, 4))
    intYear2 = VAL(RIGHT$(strDate2, 4))
   
END FUNCTION
Logged
Dantheman10110
Forum Regular
**
Posts: 109


« Reply #1 on: April 29, 2004, 09:38:15 PM »

like, number of days or just in months, days and years? will they always be after the true date, or will some dates occur before the true date? theres a lot of ways to do it.
Logged
oracle
*/-\*
*****
Posts: 3652



WWW
« Reply #2 on: April 29, 2004, 10:10:45 PM »

Welcome to the QBN forums Cheesy

Here's a tutorial put together by Moneo that explains Date and Time functions in QB very well, this may help you out:

http://qbnz.com/pages/tutorials/DATETIME.TXT
Logged

StarOnStarOff
New Member

Posts: 2


« Reply #3 on: April 29, 2004, 10:20:59 PM »

Quote
like, number of days or just in months, days and years? will they always be after the true date, or will some dates occur before the true date? theres a lot of ways to do it.

Just the number of days Cheesy And the inputted date can be before or after the computer date. I dunno where to begin :bounce:

Quote
Welcome to the QBN forums

Thanks! Cheesy Takin' a look at the link now Smiley
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!