convert days to number of months

# Thread: convert days to number of months

1. Senior Member
Join Date
Dec 1969
Posts
330

## convert days to number of months

Does anyone know of an easy way to convert a certain number of days into months?<BR>I.E. convert 45 days to 1 1/2 months<BR><BR>

2. Senior Member
Join Date
Dec 1969
Posts
2,809

## Roughly?

I take it you don&#039;t want exact, otherwise we&#039;d know WHICH 45 days right? Why not just take the number and divide by 30?

3. Senior Member
Join Date
Dec 1969
Posts
330

## RE: Roughly?

yes roughly but I&#039;m looking for an easy way to write it out in text instead of a decimal.

4. Senior Member
Join Date
Dec 1969
Posts
1,846

## RE: convert days to number of months

This depends on how many days in the month no?<BR>If your month is 31 days you could do:<BR><BR>&#060;% thedays=45<BR> &#039; Calculate days left over after dividing by days in month.<BR> remaindays=thedays mod 31 <BR> &#039; Subtract remaining days so months is divisable evenly and<BR> &#039; figure out number of months.<BR> nummonths= (thedays - remaindays)/31<BR> Response.Write("In " & thedays & " are " & nummonths & " and " & remaindays & " days.") %&#062;<BR><BR>Also if you want to find the difference of two dates you can use DateDiff. http://www.sloppycode.net/vbscript/?m=2538

5. Senior Member
Join Date
Dec 1969
Posts
330

## datediff("m","6/13/2001","3/1/2002") gives me 0 ??

Isn&#039;t my syntax correct? Why would I get 0 months?

6. Senior Member
Join Date
Dec 1969
Posts
2,809

## Convert Decimal to fraction

Public Function Dec2Frac(ByVal f)<BR> Dim df <BR> Dim lUpperPart<BR> Dim lLowerPart<BR> <BR> lUpperPart = 1<BR> lLowerPart = 1<BR> <BR> df = lUpperPart / lLowerPart<BR> While (df &#060;&#062; f)<BR> If (df &#060; f) Then<BR> lUpperPart = lUpperPart + 1<BR> Else<BR> lLowerPart = lLowerPart + 1<BR> lUpperPart = f * lLowerPart<BR> End If<BR> df = lUpperPart / lLowerPart<BR> Wend<BR>Dec2Frac = CStr(lUpperPart) & "/" & CStr(lLowerPart)<BR>End Function

7. Senior Member
Join Date
Dec 1969
Posts
330

## RE: convert days to number of months

Thanks peek that&#039;s exactly what I&#039;m looking for! Can you take it one step further and show me how I would write out the year as well if it goes over 365 days? I tried doing it after looking at your code but it would end up giving me 2.25 years, xx months, xx days if I used like 850 days.

8. Senior Member
Join Date
Dec 1969
Posts
2,809

## Might want to post code on that one

I just copied and pasted that exact code and tried it and it worked. Gave me 9.

9. Senior Member
Join Date
Dec 1969
Posts
1,846

## Works for me. Dunno what you did wrong.

I just executed it and got 9. This is the line:<BR><BR>&#060;% Response.Write(datediff("m","6/13/2001","3/1/2002")) %&#062;

10. Senior Member
Join Date
Dec 1969
Posts
330