What is the easiest way to get years, months and days?
Posted
#1
(In Topic #968)
Enthusiast

I was wondering, What is the easiest way to get years, months and days?
Let me explain, I have two specific dates: "Purchase date" and "Date of return due to breakdown" and I want to present the user, for example:
Purchase date: DD/MM/YYYY 21/12/2022
Fault Date: DD/MM/YYYY 31/10/2024
1 years, 10 months and 20 days
It is possible that these years, months and days are wrong, but it is an example.
To the question, What is the easiest way that you can think of?
Thank you and have a Happy day, I mean, a wonderful day.
For your misfortunes I am Spanish and I only know Spanish, please, be patient with me, Thank you.
Posted
Guru

To the question, What is the easiest way that you can think of?
Well, this had me thinking. 'Easy' it was not! I tried several solutions before coming up with this. This solution calculates all the days in the months between the 2 dates. For example, if the dates are 30/01/2023 to 01/03/2023 it will find all the days for January, February and March, 30 days will be deducted from the 31 of January and 30 from March leaving 1 month and 2 days.
Your example, 21/12/2022 to 31/10/2024 is 1 Year, 10 months and 10 days. See here. This routine will also produce the same result. Your example is very good as it has a leap year (2024) in it as well!
Thank you and may your 2023 be a happy one also. ¡Feliz año nuevo!Thank you and have a Happy day, I mean, a wonderful day.
Code (gambas)
- iFirstDay = 0 'Set iFirstDay
- iFirstDay = iAllDays[0] - iFirstday 'Set iFirstDay
- iAllDays.Delete(0) 'Delete the first item in the array
- iLastDay = 0 'Set iLastDay
- iAllDays.pop() 'Delete the last month form the array
- iYMD.Add(iAllDays.Count / 12) 'Calculate the years and add to the array
- iYMD.Add(iFirstDay + iLastDay) 'Calculate the days and add to the array
- Return iYMD 'Return the array
Posted
Enthusiast

I hope you are feeling very well today. Look, I take my hat off to you. Thank you.
I'm thinking of another solution but first I have to create it since it's only in my head.
Thank you for everything, and I'm sorry in some way, that I have you very busy with my problem on the date that I have put it. Sorry about it.
Greetings with my wishes to wish you Health and Happiness.
For your misfortunes I am Spanish and I only know Spanish, please, be patient with me, Thank you.
Posted
Enthusiast

Your code is failing with these dates:
DD/MM/YYYY
10/12/2022
29/12/2022
So far this is what I have verified. Greeting.
For your misfortunes I am Spanish and I only know Spanish, please, be patient with me, Thank you.
Posted
Guru

And i think you can use Val on a date string.
This works for me…
EDIT: NO IT DOES NOT!! Sorry , solution is in another post.
Code (gambas)
Code (gambas)
1 years 10 months 10 days
Posted
Guru

How it works.
Sets the 2 dates using Val()
Year comparison is rounded up so have to decrease it.
then I add the years to the older date with DateAdd()
Then compare months.
then add months to older date
then compare days.
Hope it helps
Posted
Enthusiast

My idea is similar to your logic. (It is not Work)
Code
Dim sFechaCompra As String = "21/12/2022"
Dim sFechaEstado As String = "31/10/2024"
Dim iYear As Integer = Int(DateDiff(Val(sFechaCompra), Val(sFechaEstado), gb.Month) \ 12)
Dim dFecha As Date = DateAdd(Val(sFechaCompra), gb.Year, iYear)
Dim iMes As Integer = DateDiff(dFecha, Val(sFechaEstado), gb.Month)
dFecha = DateAdd(dFecha, gb.Month, iMes)
Dim iDia As Integer = Abs(DateDiff(dFecha, Val(sFechaEstado), gb.Day))
Message(iYear & " Years " & iMes & " Months " & iDia & " Days", "OK")
I think it works for me but I'm right now testing the code with the Web that cogier has ported
For your misfortunes I am Spanish and I only know Spanish, please, be patient with me, Thank you.
Posted
Guru

Posted
Enthusiast

Your code does not work well for me, it leaks everywhere, try the date that I have given as an example of:
DD/MM/YYYY
10/12/2022
29/12/2022
My code works but I don't know if it is exact in the calculations. I am going to count the days, months and years in case it is valid.
Note: I promise you this is going to be fun, let's go your heads and mine are going to smoke.
For your misfortunes I am Spanish and I only know Spanish, please, be patient with me, Thank you.
Posted
Enthusiast

DD/MM/YYYY
10/12/2022 - 29/12/2022 0 year, 0 month, 19 days (Ok)
19/10/2021 - 09/12/2022 1 year, 1 month, 21 days (Ok)
26/09/2015 - 14/06/2021 5 year, 8 month, 20 days (Ok)
29/09/1998 - 04/10/2021 23 year, 0 month, 6 days (Ok)
For your misfortunes I am Spanish and I only know Spanish, please, be patient with me, Thank you.
Posted
Guru

(tested correct with the dates you gave in the last post)
Code (gambas)
- Dec mn
- add = 1
Just needed to note if the year/month/day being checked was before or after and adjust accordingly
Passed like this..
My Console said
0 years 0 months 19 days
1 years 1 months 21 days
5 years 8 months 20 days
23 years 0 months 6 days
Cheers for the puzzle
Posted
Enthusiast

Ole !!!
For me you are a machine. Thank you for your masterful solution
For your misfortunes I am Spanish and I only know Spanish, please, be patient with me, Thank you.
Posted
Enthusiast

With your permission, I have corrected the code because it fails with this date:
DD/MM/YYYY
04/01/2023
02/01/2026
Code
Public Sub TimeSpan(date1 As String, date2 As String) As String
Dim d1 As Date = CDate(Val(date1))
Dim d2 As Date = CDate(Val(date2))
Dim yr, mn, dy, add As Integer
yr = DateDiff(d1, d2, gb.Year)
If Month(d1) > Month(d2) Then Dec yr
If Month(d1) = Month(d2) And Day(d1) > Day(d2) Then Dec yr ' ADD
d1 = DateAdd(d1, gb.Year, yr)
mn = DateDiff(d1, d2, gb.Month)
If Day(d1) > Day(d2) Then
Dec mn
add = 1
Endif
d1 = DateAdd(d1, gb.Month, mn)
dy = DateDiff(d1, d2, gb.Day) + add
Return Subst("&1 years &2 months &3 days", yr, mn, dy)
End
For your misfortunes I am Spanish and I only know Spanish, please, be patient with me, Thank you.
1 guest and 0 members have just viewed this.



