Announcement

Collapse

Forum Guidelines

This forum is for finished source code that is working properly. If you have questions about this or any other source code, please post it in one of the Discussion Forums, not here.
See more
See less

Compare Dates

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Compare Dates

    I need to compare two dates (i.e. date1 >= date2 or Date1 <= Date2) and thought I would see if someone has some code they might share.

    Thanks.

    Manny


  • #2
    GOTO http://www.powerbasic.com/support/pb...ad.php?t=37680
    Erich Schulman (KT4VOL/KTN4CA)
    Go Big Orange

    Comment


    • #3
      Not so elegant, but simple and dirty way is to convert date to YYYY-MM-DD format and convert it to long value and then compare them:

      Code:
      MACRO YYYYMMDD_to_Numeric (dt) = VAL(LEFT$(dt,4) & MID$(dt,6,2) & RIGHT$(dt,2))
      
      MACRO YYYYMMDD_Diff (dt1, dt2) = YYYYMMDD_to_Numeric(dt2) - YYYYMMDD_to_Numeric(dt1)        
      
      FUNCTION PBMAIN () AS LONG
           
           LOCAL r1,r2 AS STRING
           r1 = "2009-08-16"
           r2 = "2009-12-31"    
           ? STR$(YYYYMMDD_Diff (r1,r2))
      
      END FUNCTION

      Comment


      • #4
        Try converting to Windows FILETIME then use a UNION or other method to get the 64bit values, then you can compare and do math on the value any way you please.
        sigpic
        Mobile Solutions
        Sys Analyst and Development

        Comment


        • #5
          Install Gregorian Library

          Why not use my Gregorian Library?
          You can simply compare two dates, ranging from 15 October 1582 (the first day of the Gregorian Calendar) to 31 December 9999, inclusive, by comparing their Gregorian Day Numbers, which are LONG INTEGERs.

          Frankly, I never thought of this possibility, so a comparision is not in the samples (as far as I remember; maybe I'll ad a wrapper later today). For the time being you must do the comparision-part yourself. You find the library on my website (for url: see my sig below).

          Egbert Zijlema, journalist and programmer (zijlema at basicguru dot eu)
          http://zijlema.basicguru.eu
          *** Opinions expressed here are not necessarily untrue ***

          Comment


          • #6
            Compare Dates

            Thanks all; I appreciate the help.

            Manny

            :happy:

            Comment


            • #7
              Compare Dates

              Using Aslan's suggestion I created the following code to compare two dates. I am sure one of you guru's can clean it up, trim it down and make it Supercalifragilisticexpialidocious. :laugh:

              Thanks again.

              HTML Code:
              DECLARE SUB month(dte$,mnth$)
                                  
              FUNCTION PBMAIN () AS LONG
              
                   DIM dt AS STRING ' original date
                   DIM dt2 AS STRING ' original date
              
                   DIM dt1 AS LONG ' re-arranged date
                   DIM dt3 AS LONG ' re-arranged date
              
                   DIM dte$, mnth$
              
                   ' Date 1
                   'dt = "2008-12-29"
                   dt = "19-JAN-2007"
                   dte$ = dt
                   mnth$ = ""
                   CALL month (dte$,mnth$)
                   dt = RIGHT$(dt,4) & mnth$ & LEFT$(dt,2)
              
                   dt = LEFT$(dt,4) & MID$(dt,5,2) & RIGHT$(dt,2)
                   dt1 = VAL(dt)
              
                   ' Date 2
                   'dt2 = "2009-12-31"
                   dt2 = "28-MAY-2008"
                   dte$ = dt2
                   mnth$ = ""
                   CALL month (dte$,mnth$)
                   dt2 = RIGHT$(dt2,4) & mnth$ & LEFT$(dt2,2)
              
                   dt2 = LEFT$(dt2,4) & MID$(dt2,5,2) & RIGHT$(dt2,2)
                   dt3 = VAL(dt2)
              
                   PRINT dt1, dt1 - dt3
                   PRINT dt3, dt3 - dt1
              
                   dte$ = ""
                   mnth$ = ""
              
                   WAITKEY$
              
              END FUNCTION
              
              SUB month(dte$, mnth$)
              
                   dte$ = MID$(dte$, 4,3)
              
                   SELECT CASE dte$
                          CASE "JAN"
                               mnth$ = "01"
                          CASE "FEB"
                               mnth$ = "02"
                          CASE "MAR"
                               mnth$ = "03"
                          CASE "APR"
                               mnth$ = "04"
                          CASE "MAY"
                               mnth$ = "05"
                          CASE "JUN"
                               mnth$ = "06"
                          CASE "JUL"
                               mnth$ = "07"
                          CASE "AUG"
                               mnth$ = "08"
                          CASE "SEP"
                               mnth$ = "09"
                          CASE "OCT"
                               mnth$ = "10"
                          CASE "NOV"
                               mnth$ = "11"
                          CASE "DEC"
                               mnth$ = "12"
                          CASE ELSE
                               mnth$ = "No Match"
                   END SELECT
              
              END SUB
              Manny :wavey:
              Last edited by Manny Corrao; 8 Feb 2009, 12:02 AM.

              Comment

              Working...
              X