home *** CD-ROM | disk | FTP | other *** search
- /***
- *
- * Date.prg
- *
- * Sample user-defined functions for manipulating dates
- * Copyright, Nantucket Corporation, 1990
- *
- * NOTE: compile with /n/w/a/m
- */
-
-
- #include "Set.ch"
-
-
-
- /***
- * Mdy( <dDate> ) --> cDate
- * Convert a date to a string in the format month dd, yyyy.
- *
- */
- FUNCTION Mdy( dDate )
- LOCAL cYear
- //
- // Handle SET CENTURY
- IF SUBSTR(SET(_SET_DATEFORMAT), -4) = "YYYY"
- cYear := STR(YEAR(dDate))
- ELSE
- cYear := " " + SUBSTR(STR(YEAR(dDate)), 4, 2)
- ENDIF
- //
- RETURN CMONTH(dDate) + " " + LTRIM(STR(DAY(dDate))) + "," + cYear
-
-
-
- /***
- * Dmy( <dDate> ) --> cDate
- * Convert a date to string formatted as dd month yyyy.
- *
- */
- FUNCTION Dmy( dDate )
- LOCAL cYear
- //
- // Handle SET CENTURY
- IF SUBSTR(SET(_SET_DATEFORMAT), -4) = "YYYY"
- cYear := STR(YEAR(dDate))
- ELSE
- cYear := " " + SUBSTR(STR(YEAR(dDate)), 4, 2)
- ENDIF
- //
- RETURN LTRIM(STR(DAY(dDate))) + " " + CMONTH(dDate) + cYear
-
-
-
- /***
- * DateAsAge( <dDate> ) --> nAge
- * Convert a date of birth to an age in years.
- *
- */
- FUNCTION DateAsAge( dDate )
- LOCAL nAge := 0
- //
- IF YEAR(DATE()) > YEAR(dDate)
- nAge := YEAR(DATE()) - YEAR(dDate)
- IF MONTH(DATE()) < MONTH(dDate) .OR.;
- ( MONTH(DATE()) = MONTH(dDate) .AND. DAY(DATE()) < DAY(dDate) )
-
- --nAge
-
- ENDIF
- ENDIF
- //
- RETURN nAge
-
-
-
- /***
- * AddMonth( <dDate>, <nMonths> ) --> dNewDate
- * Calculate a new date by adding a number of months to a given
- * date.
- *
- */
- FUNCTION AddMonth( dDate, nMonths)
- LOCAL nMonth, nDay, nYear, nLimit, nMonthAdd, nYearAdd
-
- // Break date up into its numeric components
- nMonth := MONTH( dDate )
- nDay := DAY( dDate )
- nYear := YEAR( dDate )
-
- // nLimit determines the minimum number of months that will push the
- // date into the next year. If the number of months added to the date
- // exceeds this limit, the year must be advanced by one
- nLimit := 12 - nMonth + 1
-
- // Compute number of years to add
- nYearAdd := INT( nMonths / 12 )
- IF nMonths >= nLimit
- nYearAdd++
- ENDIF
- nYear += nYearAdd
-
- // Compute number of months to add and normalize month
- nMonthAdd := nMonths % 12
- nMonth := (nMonth + nMonthAdd) % 12
-
- // Convert numeric portions to new date
- dNew := CTOD(STR(nMonth, 2) + "/" + STR(nDay, 2) + "/" + STR(nYear, 4))
-
- RETURN (dNew)
-
-
- /***
- * DateAsArray( dDate ) --> aDate
- * Convert a date to an array of year, month, and day
- *
- */
- FUNCTION DateAsArray( dDate )
- LOCAL aDate := {}
- IF VALTYPE( dDate ) != "D"
- // CAUTION: Argument error
- RETURN
- ELSE
- aDate := { YEAR( dDate ), MONTH( dDate ), DAY( dDate ) }
- ENDIF
-
- RETURN aDate
-
-
- /***
- * ArrayAsDate( aDate ) --> dDate
- * Convert an array of year, month, and day to a date value
- *
- */
- FUNCTION ArrayAsDate( aDate )
- RETURN CTOD(STR(aDate[2], 2) + "/" + STR(aDate[3], 2) + "/" + STR(aDate[1], 4))
-
-
- /***
- * DateIsLeap( <dDate> ) --> lLeap
- * Determine if the year of a supplied date is a leap year
- *
- */
- FUNCTION DateIsleap( dDate )
- LOCAL nYear := YEAR(dDate)
- RETURN ((nYear % 4) == 0) .AND. ;
- (((nYear % 100) != 0) .OR. ((nYear % 400) == 0))
-