• PicŪ Basic


  • Calculate the number of days between two dates

    There are occasions when you need to calculate the number of days between two dates. This on first thought is a complex task, what with all those variable days in a month, and leap years..

    However with a little thought the answer turn out to be relatively simple. The trick is to work from a known set point, in these examples the date is the year 1st Jan 2000, but once you understand the principals you can adapt it to your own requirements.

    There are two examples; the first is a program using Case to work out its days per month calculations. The second uses Branch, while not so easy to read it uses a 1/3rd less code.

    Please note the code below was corrected on the 22nd of March 04 due to an transcribing error.

    Tim Box

    Example Code

    'days caculation,since 1/1/2000 to 31/12/2099
    Include "proton_4.inc"

    Dim Year as Word
    Dim Month as byte
    Dim Day as byte
    dim Hour as byte
    Dim Total_Days as word
    Dim Complete_Years as byte
    Dim Complete_Years_Days as word
    Dim Current_Year_leap as byte

    Year=2005
    Month=1
    Day=1

    Total_Days=0
    Complete_Years= Year-2000 ' calculate number of completed years

    If Complete_Years>0 then
    Complete_Years_Days =Complete_Years*365+((Complete_Years-1)/4) +1
    endif

    Current_Year_leap = Complete_Years//4 ' if 0 it is leap

    select Month ' add the days of completed months
    CASE 1
    Total_Days= Complete_Years_Days
    CASE 2
    Total_Days= Complete_Years_Days + 31
    CASE 3
    Total_Days= Complete_Years_Days + 59 '31+28
    CASE 4
    Total_Days= Complete_Years_Days + 90 '31+28+31
    CASE 5
    Total_Days= Complete_Years_Days + 120 '31+28+31+30
    CASE 6
    Total_Days= Complete_Years_Days + 151 '31+28+31+30+31
    CASE 7
    Total_Days= Complete_Years_Days + 181 '31+28+31+30+31+30
    CASE 8
    Total_Days= Complete_Years_Days + 212 '31+28+31+30+31+30+31
    CASE 9
    Total_Days= Complete_Years_Days + 243 '31+28+31+30+31+30+31+31
    CASE 10
    Total_Days= Complete_Years_Days + 273 '31+28+31+30+31+30+31+31+30
    CASE 11
    Total_Days= Complete_Years_Days + 304 '31+28+31+30+31+30+31+31+30+31
    CASE 12
    Total_Days= Complete_Years_Days + 334 '31+28+31+30+31+30+31+31+30+31+30
    ENDSELECT

    Total_Days=Total_Days+ Day ' add the current month days

    IF MONTH >2 AND Current_Year_leap=0 THEN Total_Days=Total_Days+1 ' add 1 if the current year is leap, and the month >2

    print at 1,1, dec Total_Days ' total days including the current day

    Stop

    'DAYS CACULATION,SINCE 1/1/2000 TO 31/12/2099
    INCLUDE "PROTON_4.INC"

    DIM YEAR AS BYTE
    DIM MONTH AS BYTE
    DIM DAY AS BYTE
    DIM HOUR AS BYTE
    DIM TOTAL_DAYS AS WORD
    DIM COMPLETE_YEARS AS BYTE
    DIM COMPLETE_YEARS_DAYS AS WORD
    DIM CURRENT_YEAR_LEAP AS BYTE
    DIM MONTH_DAYS AS WORD
    DIM CALC_TEMP

    GOTO START

    '--- YEAR CALC SUBS ---------------
    TOTAL_CALC:
    TOTAL_DAYS = COMPLETE_YEARS_DAYS + MONTH_DAYS
    GOTO BRA_EXIT
    '----------------------------------

    CALC_DAYS:

    TOTAL_DAYS=0

    COMPLETE_YEARS= YEAR - 00 ' CALCULATE NUMBER OF COMPLETED YEARS

    IF COMPLETE_YEARS > 0 THEN
    CALC_TEMP = (COMPLETE_YEARS-1)/4)
    COMPLETE_YEARS_DAYS =COMPLETE_YEARS * 365 + CALC_TEMP +1
    ENDIF

    CURRENT_YEAR_LEAP = COMPLETE_YEARS//4 ' IF 0 IT IS LEAP

    BRANCH MONTH, [BRA_EXIT,JAN,FEB,MAR,APR,MAY,JUN,JULY,AUG,SEPT,OCT ,NOV,DECM]
    ' ADD THE DAYS OF COMPLETED MONTHS
    JAN:
    TOTAL_DAYS= COMPLETE_YEARS_DAYS
    GOTO BRA_EXIT
    FEB:
    MONTH_DAYS = 31
    GOTO TOTAL_CALC
    MAR:
    MONTH_DAYS = 59
    GOTO TOTAL_CALC
    APR:
    MONTH_DAYS = 90
    GOTO TOTAL_CALC
    MAY:
    MONTH_DAYS = 120
    GOTO TOTAL_CALC
    JUN:
    MONTH_DAYS = 151
    GOTO TOTAL_CALC
    JULY:
    MONTH_DAYS = 181
    GOTO TOTAL_CALC
    AUG:
    MONTH_DAYS = 212
    GOTO TOTAL_CALC
    SEPT:
    MONTH_DAYS = 243
    GOTO TOTAL_CALC
    OCT:
    MONTH_DAYS = 273
    GOTO TOTAL_CALC
    NOV:
    MONTH_DAYS = 304
    GOTO TOTAL_CALC
    DECM:
    MONTH_DAYS = 334
    GOTO TOTAL_CALC

    BRA_EXIT:
    TOTAL_DAYS=TOTAL_DAYS+ DAY ' ADD THE CURRENT MONTH DAYS

    IF MONTH >2 THEN
    IF CURRENT_YEAR_LEAP = 0 THEN
    TOTAL_DAYS=TOTAL_DAYS+1 ' ADD 1 IF THE CURRENT YEAR IS LEAP, AND THE MONTH >2
    ENDIF
    ENDIF
    RETURN

    START:
    YEAR=05
    MONTH=12
    DAY=1

    GOSUB CALC_DAYS

    PRINT AT 1,1, DEC TOTAL_DAYS ' TOTAL DAYS INCLUDING THE CURRENT DAY

    STOP
    contributed by Mohammed Tayem.
  • Recent Activity

    John Drew-26

    Hx711 calibration

    Thread Starter: amod

    Iam using load cell amplifier HX711 module.I have got raw values from module but iam not able to calibrate with load cell.I want make a universal...

    John Drew Yesterday, 23:40 Go to last post
    Tim-107

    PCB Stencils

    Thread Starter: SimonJA

    I've made a lot of PCB's but never used stencils so thought I would give them a try. I usually use a syringe to dispense the solder paste but it's...

    Tim Yesterday, 19:51 Go to last post