Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Code for days find
Message
From
09/10/2008 10:03:28
 
 
To
09/10/2008 00:47:31
Muthu Vel
Sty Company
India
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Environment versions
Visual FoxPro:
VFP 9
OS:
Windows XP
Network:
Windows 2008 Server
Database:
Visual FoxPro
Miscellaneous
Thread ID:
01353736
Message ID:
01353806
Views:
36
>1. How to calculate no of days between two dates?
>
>2. How to calculate" Age " between two dates?

To question 2: see below

Regards,
Sergey
*=============================================
FUNCTION AgeYMD(dt1, dt2, FlgBirth)
* Calculates difference between dt1&dt2
* dt1 - from date
* dt2 - to date
*
* FlgBirth:
* "M" - makes monthly birthday when dt2 is the last day of a month
*       and has no day from dt1. For example, for 31 March and
*       30 April it gives 1 month (not 30 days).
*       For 29,30,31 Jan and 28 Feb it gives 1 month (not 30, 29, 28 days).
*
* "Y" - applies "M" only to February months in dt1&dt2 both
*       (gives birthday for dt1=29 Feb on dt2=28 Feb in not leap year)
*
* "m" - makes monthly birthday when dt2 is the first day of a month
*       and the previous month has no day from dt1. For example, for 31 March
*       and 01 May it gives 1 month and 0 days (not 1 month and 1 day).
*       For 29,30,31 Jan and 01 March it gives 1 month 0 days.
*
* "y" - applies "m" only to February month in dt1 and March in dt2
*       (gives birthday for dt1=29 Feb on dt2=01 Mar in not leap year)
*
* Sergey Karimov, last updated 09 Jan 2008

IF EMPTY(dt1) .or. EMPTY(dt2) .or. dt1>dt2 
	RETURN ""
ENDIF

LOCAL nD0, nM0, nY0, nD1, nM1, nY1, nD2, nM2, nY2
LOCAL nShift

nD1= DAY(dt1)
nM1= MONTH(dt1)
nY1= YEAR(dt1)

nD2= DAY(dt2)
nM2= MONTH(dt2)
nY2= YEAR(dt2)

nD0= nD2-nD1 &&days

nShift= 0
DO CASE
	CASE nD0>=0 &&Ok

	CASE nD1<29 &&just skip useless calculations
	CASE EMPTY(FlgBirth) &&sorry, birthday only day on day

	CASE FlgBirth$"ym"
IF (FlgBirth="m" .or. (nM1=2 .and. nM2=3)) .and. DAY(dt2-nD2)<nD1
	nShift= 1
ENDIF

	CASE FlgBirth="Y" .and. (nM1#2 .or. nM2#2) &&not February
	CASE MONTH(dt2+1)=nM2 &&dt2 is not the last day of a month

	OTHERWISE &&happy birthday!
nD0= 0
ENDCASE

IF nD0<0
	nD0= MAX(0,DAY(dt2-nD2)-nD1)+nD2-nShift &&rest of days in the previous month + D2
	nM2= nM2-1
ENDIF

nM0= nM2-nM1 &&months
IF nM0<0
	nM0= nM0+12
	nY2= nY2-1
ENDIF

nY0= nY2-nY1 &&years

RETURN STR(nY0,4)+"/"+STR(nM0,2)+"/"+STR(nD0,2)
Previous
Reply
Map
View

Click here to load this message in the networking platform