What is the easiest way to get years, months and days?

New to Gambas? Post your questions here. No question is too silly or too simple.
User avatar
gambafeliz
Posts: 139
Joined: Friday 2nd September 2022 7:50pm
Location: I live in the same city as Picasso

What is the easiest way to get years, months and days?

Post by gambafeliz »

Happy New Year

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. :)
User avatar
cogier
Site Admin
Posts: 1118
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: What is the easiest way to get years, months and days?

Post by cogier »

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 have a Happy day, I mean, a wonderful day.
Thank you and may your 2023 be a happy one also. ¡Feliz año nuevo!

Public Sub Form_Open()

  Dim iYMD As Integer[]

  iYMD = GetPeriod(Date(2022, 12, 21), Date(2024, 10, 31))

  Message(Str(iYMD[0]) & " Years " & Str(iYMD[1]) & " Months " & Str(iYMD[2]) & " Days", "OK")

End

Public Sub GetPeriod(dDate1 As Date, dDate2 As Date) As Integer[]                   'Returns the period between the first date and the last date in the format "YMD"

  Dim iMonths As Integer[] = [0, 31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]     'Amount of days in each month
  Dim iAllDays As New Integer[]                                                     'Array of all the days in each month
  Dim iYMD As New Integer[]                                                         'To return in the format "YMD"
  Dim iFirstday As Integer = Day(dDate1)                                            'First day of the first date
  Dim iLastDay As Integer = Day(dDate2)                                             'Last day of the last date
  Dim iFirstMonth As Integer = Month(dDate1)                                        'First month of first date
  Dim iLastMonth As Integer = Month(dDate2)                                         'Last month of last date
  Dim iFirstYear As Integer = Year(dDate1)                                          'First Year of first date
  Dim iLastYear As Integer = Year(dDate2)                                           'Last Year of first date
  Dim dWorkDate As Date = Date(iFirstYear, iFirstMonth, 1)                          'Used for calculations
  Dim dFebCheck As Date                                                             'Used to check if February is 28 or 29 days

  Do
    If Month(dWorkDate) = 2 Then                                                    'Is the month February?
      Try dFebCheck = Date(Year(dWorkDate), 2, 29)                                  'Does February work?
      If Error Then iMonths[2] = 28 Else iMonths[2] = 29                            'If no then 28 days else 29 days
    Endif
    iAllDays.Add(iMonths[Month(dWorkDate)])                                         'Add the amount of days in the month to the array
    If Year(dWorkDate) = iLastYear And Month(dWorkDate) = iLastMonth Then Break     'If the end date has been reached then break out of the loop
    dWorkDate = DateAdd(dWorkDate, gb.Month, 1)                                     'Add one month to the calculation and continue
  Loop

  If iFirstday = iAllDays[0] Then                                                   'If the first day is the same as the last day of the first month then
    iFirstDay = 0                                                                   'Set iFirstDay
  Else
    iFirstDay = iAllDays[0] - iFirstday                                             'Set iFirstDay
  Endif

  iAllDays.Delete(0)                                                                'Delete the first item in the array

  If iLastDay = iAllDays[iAllDays.max] Then                                         'If the last day = the last day of the last month then
    iLastDay = 0                                                                    'Set iLastDay
  Else
    iAllDays.pop()                                                                  'Delete the last month form the array
  Endif

  iYMD.Add(iAllDays.Count / 12)                                                     'Calculate the years and add to the array
  iYMD.Add(iAllDays.Count Mod 12)                                                   'Calculate the months and add to the array
  iYMD.Add(iFirstDay + iLastDay)                                                    'Calculate the days and add to the array

  Return iYMD                                                                       'Return the array

End
User avatar
gambafeliz
Posts: 139
Joined: Friday 2nd September 2022 7:50pm
Location: I live in the same city as Picasso

Re: What is the easiest way to get years, months and days?

Post by gambafeliz »

Hi cogier.

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. :)
User avatar
gambafeliz
Posts: 139
Joined: Friday 2nd September 2022 7:50pm
Location: I live in the same city as Picasso

Re: What is the easiest way to get years, months and days?

Post by gambafeliz »

Hi cogier

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. :)
User avatar
BruceSteers
Posts: 1521
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: What is the easiest way to get years, months and days?

Post by BruceSteers »

What about DateDiff ?
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.
Public Sub TimeSpan(date1 As String, date2 As String) As String

  Dim d1 As Date = Date(Val(date1))
  Dim d2 As Date = Date(Val(date2))
  Dim yr, mn, dy As Integer

  yr = DateDiff(d1, d2, gb.Year) - 1
  d1 = DateAdd(d1, gb.Year, yr)
  mn = DateDiff(d1, d2, gb.Month)
  d1 = DateAdd(d1, gb.Month, mn)
  dy = DateDiff(d1, d2, gb.Day)

  Return Subst("&1 years &2 months &3 days", yr, mn, dy)

End

Using your dates like this..
Print TimeSpan("21/12/2022", "31/10/2024")

gives me this...

1 years 10 months 10 days
If at first you don't succeed , try doing something differently.
BruceS
User avatar
BruceSteers
Posts: 1521
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: What is the easiest way to get years, months and days?

Post by BruceSteers »

I edited the above routine because it was wrong.

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 :)
If at first you don't succeed , try doing something differently.
BruceS
User avatar
gambafeliz
Posts: 139
Joined: Friday 2nd September 2022 7:50pm
Location: I live in the same city as Picasso

Re: What is the easiest way to get years, months and days?

Post by gambafeliz »

Happy New Year BruceS

My idea is similar to your logic. (It is not Work)

Code: Select all

	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
Last edited by gambafeliz on Monday 2nd January 2023 8:09am, edited 1 time in total.
For your misfortunes I am Spanish and I only know Spanish, please, be patient with me, Thank you. :)
User avatar
BruceSteers
Posts: 1521
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: What is the easiest way to get years, months and days?

Post by BruceSteers »

Me ideas are all wrong sorry.
If at first you don't succeed , try doing something differently.
BruceS
User avatar
gambafeliz
Posts: 139
Joined: Friday 2nd September 2022 7:50pm
Location: I live in the same city as Picasso

Re: What is the easiest way to get years, months and days?

Post by gambafeliz »

Hi BruceS

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. :)
User avatar
gambafeliz
Posts: 139
Joined: Friday 2nd September 2022 7:50pm
Location: I live in the same city as Picasso

Re: What is the easiest way to get years, months and days?

Post by gambafeliz »

I am going to give you dates calculated by the web provided previously.

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)
Last edited by gambafeliz on Monday 2nd January 2023 8:08am, edited 1 time in total.
For your misfortunes I am Spanish and I only know Spanish, please, be patient with me, Thank you. :)
Post Reply