ciginfo Well-Known Member Licensed User Longtime User Sep 12, 2011 #1 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.
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 Sep 12, 2011 #2 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
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.
thedesolatesoul Expert Licensed User Longtime User Sep 12, 2011 #3 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 Code: Copy 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
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 Code: Copy 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
K kickaha Well-Known Member Licensed User Longtime User Sep 12, 2011 #4 A good routine, but it fails for leap years. Upvote 0
thedesolatesoul Expert Licensed User Longtime User Sep 12, 2011 #5 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 Code: Copy 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
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 Code: Copy 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
thedesolatesoul Expert Licensed User Longtime User Sep 12, 2011 #6 And for GetDaysinMonth Code: Copy 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
And for GetDaysinMonth Code: Copy 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
K kickaha Well-Known Member Licensed User Longtime User Sep 12, 2011 #7 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
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.
thedesolatesoul Expert Licensed User Longtime User Sep 12, 2011 #8 kickaha said: 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. Click to expand... You are correct. It should be: Code: Copy if Month = 13 then Just to let you know...i have never written code that works first time around Upvote 0
kickaha said: 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. Click to expand... You are correct. It should be: Code: Copy if Month = 13 then Just to let you know...i have never written code that works first time around
K kickaha Well-Known Member Licensed User Longtime User Sep 12, 2011 #9 thedesolatesoul said: You are correct. It should be: Code: Copy if Month = 13 then Just to let you know...i have never written code that works first time around Click to expand... What I meant was if you start this code block with diff_days = 29 month = 11 (December) Code: Copy 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: Code: Copy 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 Click to expand... Last edited: Sep 12, 2011 Upvote 0
thedesolatesoul said: You are correct. It should be: Code: Copy if Month = 13 then Just to let you know...i have never written code that works first time around Click to expand... What I meant was if you start this code block with diff_days = 29 month = 11 (December) Code: Copy 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: Code: Copy 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 Click to expand...
thedesolatesoul Expert Licensed User Longtime User Sep 12, 2011 #10 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] Upvote 0
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]
K kickaha Well-Known Member Licensed User Longtime User Sep 12, 2011 #11 thedesolatesoul said: 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] My code corrected for that Click to expand... Upvote 0
thedesolatesoul said: 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] My code corrected for that Click to expand...