Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Challenge: Compute number of days between two dates.
#1
Given:
1) Two date strings, D1$ and D2$.
2) They should be in the format: YYYYMMDD.
3) You must check them for validity, e.g., 20030229 and 20030931 and 1999019 are all invalid.
4) Once they're valid, figure out how to compute the number of days (positive or negative) of D2$ minus D1$.
*****
Reply
#2
Come on, let's do something more fun than parsing. Blehh.
am an asshole. Get used to it.
Reply
#3
NINKAZU,
Why do you consider this as a parsing problem? Only a small part the date validation could be considered parsing. The rest is computational.
*****
Reply
#4
Quote:NINKASU
:evil:
am an asshole. Get used to it.
Reply
#5
Sorry 'bout that --- I fixed it.
*****
Reply
#6
I am becoming way too addicted to these programming challenges. I have so much other stuff I should be doing..... :-?

Anyways, here's my entry for the Days-Between-Two-Dates challenge. I hope it works alright!

*peace*

Meg.

Code:
DECLARE FUNCTION TotDays& (Start$, End$)
DECLARE FUNCTION EnterDate$ (Prompt$)
DECLARE FUNCTION DInM% (Month%, Year%)

     '**********************************************************************
     ' Days between two dates calculator.  Written by Meg Berry, July 2003.
     '**********************************************************************

CLS
D1$ = EnterDate$("First Date")
D2$ = EnterDate$("Second Date")

PRINT : PRINT "There are ";
IF D1$ > D2$ THEN
     PRINT -TotDays&(D2$, D1$);
ELSEIF D2$ > D1$ THEN
     PRINT TotDays&(D1$, D2$);
ELSE
     PRINT 0;
END IF
PRINT "days between those dates."

SYSTEM

FUNCTION DInM% (m%, y%)

     '*************************** OBJECTIVE ********************************
     'This function determines the number of days in a given month and year.
     '**********************************************************************

     '************************* ARGUMENT LIST ******************************
     'm%            month to check
     'y%            year to check
     '**********************************************************************

     '*** DETERMINE USUAL DAYS IN THE MONTH ***
     DInM% = 31 + (m% MOD 2) * (m% > 7) + (m% MOD 2 XOR 1) * (m% < 8)
    
     '*** IF FEB. THEN ADJUST DAYS DEPENDING ON LEAP YEAR ***
     IF m% = 2 THEN DInM% = 28 - ((y% MOD 4 = 0) + (y% MOD 100 = 0) * NOT (y% MOD 400 = 0))
    
END FUNCTION

FUNCTION EnterDate$ (Prompt$)

     '*************************** OBJECTIVE ********************************
     'This function returns a valid date string
     '**********************************************************************
    
     '************************* ARGUMENT LIST ******************************
     'Prompt$       Message passed to be displayed before the INPUT command
     '**********************************************************************

     '************************* VARIABLE LIST ******************************
     'EM$           Standard error message
     'd$            Temporary storage of date
     'check$        String used to check for non-numeric characters
     'y%            year, extracted from d$
     'm%            month, extracted from d$
     'd%            day, extracted from d$
     '**********************************************************************

     Em$ = "  Please enter a date between 00010101 and 99991231."
    
     DO
          PRINT
          PRINT Prompt$
          LINE INPUT "Enter Date (yyyymmdd): ", d$
          FOR Temp% = 1 TO 1
               IF LEN(d$) <> 8 THEN PRINT "Invalid Length." + Em$: EXIT FOR
               check$ = RIGHT$(STR$(INT(VAL(d$))), LEN(STR$(INT(VAL(d$)))) - 1)
               check$ = LEFT$("00000000", LEN(d$) - LEN(check$)) + check$
               IF check$ <> d$ THEN PRINT "Invalid Character." + Em$: EXIT FOR
               y% = VAL(LEFT$(d$, 4))
               m% = VAL(MID$(d$, 5, 2))
               d% = VAL(RIGHT$(d$, 2))
               IF y% < 1 THEN PRINT "Invalid Year." + Em$: EXIT FOR
               IF m% < 1 OR m% > 12 THEN PRINT "Invalid Month." + Em$: EXIT FOR
               IF d% < 1 OR d% > DInM%(m%, y%) THEN PRINT "Invalid Day." + Em$: EXIT FOR
               EXIT DO
          NEXT Temp%
     LOOP
    
     EnterDate$ = d$

END FUNCTION

FUNCTION TotDays& (S$, E$)
     '*************************** OBJECTIVE ********************************
     'This function returns the number of days between a start and end date.
     '**********************************************************************

     '************************* ARGUMENT LIST ******************************
     'S$            starting date
     'E$            ending date
     '**********************************************************************

     '************************* VARIABLE LIST ******************************
     'Sy% Sm% Sd%   starting year, month, and day
     'Ey% Em% Ed%   ending year, month, and day
     'Count&        day counter
     '**********************************************************************
    
     Sy% = VAL(LEFT$(S$, 4))
     Ey% = VAL(LEFT$(E$, 4))
     Sm% = VAL(MID$(S$, 5, 2))
     Em% = VAL(MID$(E$, 5, 2))
     Sd% = VAL(RIGHT$(S$, 2))
     Ed% = VAL(RIGHT$(E$, 2))

     IF Sy% = Ey% AND Sm% = Em% THEN
          Count& = Ed% - Sd%
     ELSE
          Count& = DInM%(Sm%, Sy%) - Sd% + Ed%
          DO UNTIL ((Sy% = Ey%) AND (Sm% = Em% - 1)) OR ((Sy% = Ey% - 1) AND (Sm% = 12) AND (Em% = 1))
               Sm% = Sm% + 1
               IF Sm% = 13 THEN Sm% = 1: Sy% = Sy% + 1
               Count& = Count& + DInM%(Sm%, Sy%)
          LOOP
     END IF

     TotDays& = Count&
END FUNCTION
Reply
#7
is there a way to make posts so that they don't wrap in the post box? I thought I've seen posts that had horizontal scroll bars?

I hate when the forum word-wraps my code :p

*peace*

Meg.
Reply
#8
Whenever I post a long line of code in [code] tags, it doesn't bother line-wrapping it. That's why you see scroll bars. But it doesn't really matter does it? All people to is copy and paste the code into notepad and it's correctly formatted.
Reply
#9
Quote:...Anyways, here's my entry for the Days-Between-Two-Dates challenge. I hope it works alright!
Does that mean that you haven't checked it out yourself? :wink:

MEG: You are incredible! I checked your program out and it works exactly to specifications. Did you do this unbelievable program from scratch in less than 4-5 hours, or did you "lift" some logic from some of your other programs?

In any event, great job! Tongue
*****
Reply
#10
I coded the thing from scratch. It took about an hour and a half. I had to look up how to determine a leap year, but the logic line for it (the two lines in the DInM% function) are original.

There actually were a couple logic errors in the original post (notice it's been edited two times hehehe). I think they're worked out, now.

*peace*

Meg.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)