No announcement yet.

Date Functions

  • Filter
  • Time
  • Show
Clear All
new posts

    Date Functions

    Are there any Date functions besides Date$ in PB/CC?
    I am trying to do a very small data base where I input a date and then records are created for the next 2 week period?
    Thanks, Lynn

    There are no built-in functions, but many implementations of date routines that you can find using the Forum search tool. You can also use Google and enter date routines and find leads that way.

    Be aware that the normal process of handling dates is to convert dates to a reference number, and that by subtracting one reference number from another you can determine the number of days between two dates. Or you could add a quantity like 14 to a reference number and convert it back to a date to establish the date 14 days from now. Some refer to the reference number as a daycount. Since every week is exactly 7 days, you can even determine the day of the week using a MOD 7 and getting a remainer of 0 to 6, then relating that to a day of the week.

    You could also write your own routines, but it is a bit of a bother. The rule is that there are 365 days in normal years, plus one extra day (February 29th) in the following years: All years divisible evenly by 400, or any year that is divisible evenly by 4 that is not also divisible evenly by 100. You generally use the MOD operator to check for even divisibility, so if (year MOD 400) equals zero, you have a leapyear. Or if (year MOD 100) is not equal to zero and (year MOD 4) is equal to zero, you again have a leapyear, Otherwise, not a leapyear.

    Daycount is generally based on four year intervals as a start. As one year is approximately equal to 365.2425 days (count (365 * 4 + 1), then divide by 4 for average days over four consecutive years, it is often easier to begin by adding the total of 365 *4 + 1 (which is 1461) and deducting four years from the given year, until you get to the last few years, then figure them individually. A faster way is to perform an integer division with 4 (year\4), and take that result and multiply it by 1461 to get a daycount up to the nearest leapyear. Then take from what is left (year MOD 4) to find out how many remaining days to add in based on a month-by-month accessment. If the given month is January, you add 0. If the given month is February, you add 31 (the days found in the preceeding January) If the month is after February (most are), you add another if this is a leapyear. Then for March you add 28 for February and 31 for January, which is 59 total (a leapyear would make it 60). That's all there is to it. If the year MOD 4 is 0, and you are still in January or February, you deduct one - its leapyear, but you are not yet beyond leapday. If any other result, you add 365 * (year MOD 4).

    Then test, test, test, to make sure you got it right and made no mistakes. Otherwise, just use someone else's code and hope that they tested, tested, tested until they got it right.



      Egbert Zijlema has an extensive library of date functions.


        Then test, test, test, to make sure you got it right and made no mistakes. Otherwise, just use someone else's code and hope that they tested, tested, tested until they got it right.
        Testing the mettle of a programmer.

        In some future era, dark matter and dark energy will only be found in Astronomy's Dark Ages.


          Date algorithm


          I am not sure if this is what you are looking for, but there is an algorithm that calculates a general number for a date, and that can be used for all sorts of calendar purposes. It handles leap years and the month of februari correctly.

          It comes from an old Hewlett Packard manual, where it was presented as an application example.

          N(j,m,d) = INT(365.25*g(j,m)) + INT(30.6*f(m)) + d

          N = number assigned to a date
          j = year
          m = month
          d = day

          if m > 2 then : f(m) = m + 1, g(j,m) = j

          else : f(m) = m + 13, g(j,m) = j - 1

          Arie Verheul


            User to user discussion about the PowerBASIC for DOS product line. Includes discussions about PBDK, QuickPak Pro, PB/Vision, PB/Xtra, and PowerTree for DOS.

            Paul Purvis routines: