How many months between two dates ?

ciginfo

Well-Known Member
Licensed User
Longtime User
Bonjour,
How many months between two dates ?
Does DateTime.TicksPerMonth exist like DateTime.TicksPerDay ?
i should want to know how many months and days between two dates.
Ex : There are 8 months and 14 days between Xdate and Ydate.
Thank you.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
TicksPerMonth doesn't exist as the value of ticks per month is not constant.

I actually started creating an example for this question, and I realized that it is not completely defined.
For example what will you answer for the number of months and days between Janurary 10 to February 20?

Depending on your scenario you can just calculate the days and assume that a month is 30 days.
 
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
To find out the months in between dates you need to know exactly WHICH months occur between the dates. Its a little bit complicated.

What would be easier is to walk through the months

PSUEDOCODE:
If diff_days = the difference in days
and Month = starting Month
diff_months = 0
B4X:
Do While(diff_days > 28)
   diff_days = diff_days - GetDaysinMonth(Month)
   diff_months = diff_months + 1
   Month = Month + 1
   if Month = 12 then Month = 0
   if diff_days <= 31 then
      k = GetDaysinMonth(Month)
      if k > diff_days then exit
   end if
   end if
Loop
 
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
Let me try again :)

PSUEDOCODE:
If diff_days = the difference in days
and Month = starting Month
and Year = Starting Year
diff_months = 0
GetDaysinMonth returns the number of days in a given month and also takes care of Leap years


B4X:
Do While(diff_days > 28)
   diff_days = diff_days - GetDaysinMonth(Month,Year)
   diff_months = diff_months + 1
   Month = Month + 1
   if Month = 12 then 
             Month = 0
             Year = Year + 1
   end if
   if diff_days <= 31 then
      k = GetDaysinMonth(Month,Year)
      if k > diff_days then exit
   end if
Loop
 
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
And for GetDaysinMonth

B4X:
sub GetDaysinMonth(Month,Year) as int
if Year Mod 4 = 0 then
       if Year Mod 100 = 0 then
               if Year Mod 400 = 0 then
                       is_leap_year = true
               else  
                       is_leap_year = false
       else 
               is_leap_year = true
else
       is_leap_year = false
end if
if Month = 2 then
    if is_leap_year = true then
         return 29
    else 
         return 28
    end if
else if Month = 1 or Month = 3 or Month = 5 or Month = 6 or Month = 7 or Month = 10 or Month = 12 then
     return 31
else 
     return 30
end if
end sub

By the way...I have just written this from the top of my head
this code isnt tested at all :)
 
Upvote 0

kickaha

Well-Known Member
Licensed User
Longtime User
On further reflection on your code, you would also have a false result if (for example) you started with 29 days and the start month was December.
 
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
On further reflection on your code, you would also have a false result if (for example) you started with 29 days and the start month was December.

You are correct.
It should be:
B4X:
if Month = 13 then

Just to let you know...i have never written code that works first time around :)
 
Upvote 0

kickaha

Well-Known Member
Licensed User
Longtime User
You are correct.
It should be:
B4X:
if Month = 13 then

Just to let you know...i have never written code that works first time around :)

What I meant was if you start this code block with
diff_days = 29
month = 11 (December)

B4X:
Do While(diff_days > 28)
   diff_days = diff_days - GetDaysinMonth(Month,Year)
   diff_months = diff_months + 1
   Month = Month + 1
   if Month = 12 then 
             Month = 0
             Year = Year + 1
   end if
   if diff_days <= 31 then
      k = GetDaysinMonth(Month,Year)
      if k > diff_days then exit
   end if
Loop

What would happen is :

diff_days = -2 (29 - 31)
diff_months = 1

This:
B4X:
Do While(diff_days > 0)
   If GetDaysinMonth(Month,Year) > diff_days then exit
   diff_days = diff_days - GetDaysinMonth(Month,Year)
   diff_months = diff_months + 1
   Month = Month + 1
   if Month = 13 then 
      Month = 1
      Year = Year + 1
   end if
Loop
solves the problem, although I have the same talent as you
Just to let you know...i have never written code that works first time around
 
Last edited:
Upvote 0
Top