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.
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.
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
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
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
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
oh...u have a compiler in your brain
i think there will be more errors but i will leave the users to solve those
[The GetDaysinMonth function I posted looks at months 1-12 rather than 0-11]
oh...u have a compiler in your brain
i think there will be more errors but i will leave the users to solve those [The GetDaysinMonth function I posted looks at months 1-12 rather than 0-11][/QUOTE]