Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Number to Years, Months, Weeks, Days 1

Status
Not open for further replies.

Svenos

Programmer
Nov 17, 2003
7
GB
I need a formula which will convert a number (which are days), to Years, months, weeks and days.

Having looked in the FAQ's, the examples all seem to require a start and end date which I don't have.

I'm using Crystal Reports XI.

Any help greatly appreciated.
 
366 days would equal 1 year 1 day. So you don't need a start date and end date.

So revisit the FAQ, they are probably subtracting the start and end dates. Just replace that logic with your number field.

Software Sales, Training, Implementation and Support for Macola, eSynergy, and Crystal Reports

"What version of URGENT!!! are you using?
 
Thanks for the reply dgillz. The example code I found is this;

datetimevar min := minimum({@Character field to date field in process date});
datetimevar max := maximum({@Character field to date field in process date});
numbervar yrs := datediff("yyyy",min,max);
numbervar mos;
if month(max) >= month(min) then
mos := datediff("m",min,max)-(yrs*12) else
if month(max) < month(min) then (
yrs := yrs-1;
mos := datediff("m",min,max)-(yrs*12)
);
numbervar days :=
if day(max)>= day(min) then
days := day(max) - day(min) else
if day(max) < day(min) then (
mos := mos-1;
days := day(max)+(day(max-day(max))-day(min))
);
totext(yrs,0,"")+" Yrs "+totext(mos,0,"")+" Mos "+totext(days,0,"")+ " Days"

From the advice you have given, this appears to be very reliant on there being a start and end date so it not so simple as replacing the dates with a number.

Do you have some more appropriate code for my problem?
 
Try this:

numbervar x := {table.number};
numbervar yrs;
numbervar mos;
numbervar wks;
numbervar days;

yrs := truncate(x/365.25);
mos := truncate(remainder(x,365.25)/30.4375);
wks := truncate(remainder(remainder(x,365.25),30.4375)/7);
days := truncate(remainder(remainder(remainder(x,365.25),30.4375),7));

totext(yrs,0,"") +" yrs "+
totext(mos,0,"") +" mos " + totext(wks,0,"")+ " wks " +
totext(days,0,"") + " days"

This assumes that months and years are all of the same length, which they are not, of course, but it would be correct on the average.

-LB
 
Ibass, you are a star!

This is exactly what I am trying to do.

Thank you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top