Print Page | Close Window

Calculating months [RESOLVED]

Printed From: Crystal Reports Book
Category: Crystal Reports 9 through 2020
Forum Name: Report Design
Forum Discription: The best way to design a report and problems you have encountered
URL: http://www.crystalreportsbook.com/forum/forum_posts.asp?TID=6253
Printed Date: 27 Apr 2024 at 4:45am


Topic: Calculating months [RESOLVED]
Posted By: cajsoft
Subject: Calculating months [RESOLVED]
Date Posted: 05 May 2009 at 12:57am

Hi,

I require a formula in CRX to work out the no of complete months between to dates.
 
I dont want to use the datediff function as it doesnt evaulate the dates properly.
 
ie,  stardate is 25/9/2008  enddate is 05/05/2009
 
datediff returns  20  which is incorrect.. it should be 19 complete months
 
hope someone can help
 
Craig.



Replies:
Posted By: cajsoft
Date Posted: 05 May 2009 at 2:37am
I found the answer myself:
WhilePrintingRecords;
//Years
//0.25 accounts for leap year
numbervar Y := (currentdate- {BIRTHDATE})/365.25;
//Months
//30.4375 = 365.25/12 average number of days in a month
numbervar M := Remainder((currentdate- {BIRTHDATE}),365.25)/30.4375;
// check for Months = 12
if truncate(M) = 12
then months := truncate(Y)+1
else months := (truncate(Y) * 12 + truncate(M) );



Print Page | Close Window