Results 1 to 17 of 17

Thread: [RESOLVED] Calculate Age-February issue

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Feb 2021
    Location
    Greece
    Posts
    17

    Resolved [RESOLVED] Calculate Age-February issue

    Hello everybody. I managed to create a function which calculates the age, based on today's date in form of "X years + Y months+ Z days". For example if someone is born at November 23rd 1978, it returns "42 years old + 3 months + 2 days" The only issue i had is when today's month is February. Although i used "DaysInMonth" method to get the days of the current month of the current year, there was a slightly small difference of 3 days in results based in comparison with Windows calculator results. Then, according to my calculations i realized that what Windows Calculator returns, seems to be that is assumed that February has 31 days. I changed that in my function too, and the results are exactly the same. I checked both Windows Calculator with my function, as well as several sites that calculate date difference in the above return type and the results are completely the same. Do you know why this happens? Is there an international convention or something that defines February with 31 days, for these kind of calculations?
    Last edited by niktsol; Feb 25th, 2021 at 01:53 PM.

  2. #2
    Frenzied Member
    Join Date
    Nov 2017
    Posts
    1,459

    Re: Calculate Age-February issue

    I've never come across anything that assumes February has 31 days.

    It is hard to help you fix your function when you don't post any code.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Feb 2021
    Location
    Greece
    Posts
    17

    Re: Calculate Age-February issue

    Code:
    Function Age(ByVal yr As Int32, ByVal Mn As Int32, ByVal Dy As Int32) As Int32
    
    
            Dim cur_Year As Int32 = DateTime.Now.Year
            Dim cur_month As Int32 = DateTime.Now.Month
            Dim cur_day As Int32 = DateTime.Now.Day
            Dim rest_year As Int32
            Dim rest_month As Int32
            Dim rest_day As Int32
    
    
    
            'compute the integer difference between years
            rest_year = cur_Year - yr
            'if current month and current day are equal with given date's month and day return the year no need to calculate anything else
          [INDENT]  If cur_month = Mn And cur_day = Dy Then
                      MessageBox.Show("You are" + rest_year.ToString() + "years old. ", "Age Results", MessageBoxButtons.OK, MessageBoxIcon.Information)
                      Return rest_year
            End If
    
            'if the current day is less than given date's day then we add the days of the current month of the given date day and
            'decrease the value of month by 1
    
           If cur_day < Dy Then
                       If cur_month = 2 Then
                           cur_day += 31
                       Else
                            cur_day += DateTime.DaysInMonth(cur_Year, cur_month)
                      End If
                      cur_month -= 1
            End If
            'If the  current month Is less than given Date's month then we add the month 12 months and
            'decrease the value of year by 1
    
            If cur_month < Mn Then
                 cur_month += 12
                rest_year -= 1
            End If
    
            rest_month = cur_month - Mn
            rest_day = cur_day - Dy
            MessageBox.Show("You are" + rest_year.ToString() + " years old, " + rest_month.ToString() + " months " + rest_day.ToString() + " days.", "Age Results", MessageBoxButtons.OK, MessageBoxIcon.Information)
    
    
    
            Return rest_year
    
        End Function
    Last edited by dday9; Feb 26th, 2021 at 09:35 AM. Reason: Added Code Tags

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Feb 2021
    Location
    Greece
    Posts
    17

    Re: Calculate Age-February issue

    This is what i've done so far, and returns exactly the same results as Windows Calculator. The function call is like Age(1978,11,23). So far, returns only the age based in calculations, that's why i have placed a message box to show months and days.
    Last edited by niktsol; Feb 25th, 2021 at 02:11 PM.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Feb 2021
    Location
    Greece
    Posts
    17

    Re: Calculate Age-February issue

    My apologies, for the indentation. Although i use indentation at code editor and try to fix it by editing the post, it seems that is does not take the indentation changes in the post

  6. #6
    Frenzied Member
    Join Date
    Nov 2017
    Posts
    1,459

    Re: Calculate Age-February issue

    Code:
    If cur_day < Dy Then
        If cur_month = 2 Then
            cur_day += 31
        Else
            cur_day += DateTime.DaysInMonth(cur_Year, cur_month)
        End If
        cur_month -= 1
    End If
    ...
    rest_day = cur_day - Dy
    The flaw in your logic is that you seem to think that the total number of days in the current month is relevant in your calculations, which isn't the case.

    Imagine for a second that February had 5 days and January had 31. Ignoring your February "workaround", if today was February 4th and my birthdate was January 30th, by using your logic, the calculated age is something like -21 days.

    In the case that the current day number is less than the given day number, you need to use the total number of days in the *given* month when calculating the day difference, not the values in the *current* month. And when you do that, you don't need any additional logic specific to February.
    Last edited by OptionBase1; Feb 25th, 2021 at 02:28 PM.

  7. #7
    Frenzied Member jdc2000's Avatar
    Join Date
    Oct 2001
    Location
    Idaho Falls, Idaho USA
    Posts
    1,810

    Re: Calculate Age-February issue

    My apologies, for the indentation. Although i use indentation at code editor and try to fix it by editing the post, it seems that is does not take the indentation changes in the post.
    Use the Code tags (#) symbol in the tools when posting to enclose the code to preserver the indenting.

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Feb 2021
    Location
    Greece
    Posts
    17

    Re: Calculate Age-February issue

    Quote Originally Posted by OptionBase1 View Post
    The flaw in your logic is that you seem to think that the total number of days in the current month is relevant in your calculations, which isn't the case.

    Imagine for a second that February had 5 days and January had 31. If today was February 4th and my birthdate was January 30th, by using your logic, the calculated age is something like -21 days.

    In the case that the current day number is less than the given day number, you need to use the total number of days in the *given* month when calculating the day difference, not the values in the *current* month.
    One moment. I am calculating the months based at the compound numbers theory. I hope that it is the correct translation in English. Let me demonstrate you the following example.

    Let's "subtract" this dates on paper February 25th 2021 minus November 23rd 1978.


    2021 2 25
    1978 11 23


    So at days:25-23=2
    Months 2-11 is less that 0 so i subratct one year (2020) and add 12 months to February so it is now 14. So 14-11=3
    Years:2020(beacuse we subtracted on years above)-1978=42

    So the result is 42 years , 3 months and 2 days.

    This option is based at the fact that all months have 30 or 31 days, or 28 or 29 (February leap year or not)

    That's how i calculate with the above function. I've tested over several cases in Windows Calculator.

  9. #9
    Frenzied Member
    Join Date
    Nov 2017
    Posts
    1,459

    Re: Calculate Age-February issue

    Fantastic. That example doesn't address my post at all.

    Good luck.

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Feb 2021
    Location
    Greece
    Posts
    17

    Re: Calculate Age-February issue

    Quote Originally Posted by jdc2000 View Post
    Use the Code tags (#) symbol in the tools when posting to enclose the code to preserver the indenting.
    Thanks a lot. I'll use it next time.

  11. #11
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Pointless Forest 38.517,-92.023
    Posts
    9,363

    Re: Calculate Age-February issue

    You should search 'Calculate Age' on this site. Top right, in the banner. We had a rather lengthy discussion about it several years ago. Actually there have been several.
    Last edited by dbasnett; Feb 25th, 2021 at 03:46 PM.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  12. #12

    Thread Starter
    Junior Member
    Join Date
    Feb 2021
    Location
    Greece
    Posts
    17

    Re: Calculate Age-February issue

    Quote Originally Posted by dbasnett View Post
    You should search 'Calculate Age' on this site. Top right, in the banner. We had a rather lengthy discussion about it several years ago. Actually there have been several.
    I see. Thanks for your post. I will search it.

  13. #13
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    2,725

    Re: Calculate Age-February issue

    did you try and use the GregorianCalendar ?

    see if you can use this
    Code:
     Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            Dim dc As New System.Globalization.GregorianCalendar()
            Dim formatInfo = System.Globalization.DateTimeFormatInfo.CurrentInfo
            ListBox1.Items.Add("Year" & "-" & "Days " & "-" & "Month ")
            ListBox1.Items.Add("-----------------------------------------")
    
            'write the result to a Textfile
            Using sw As New IO.StreamWriter("E:\TestFolder\TestfileYear_Days.txt")
                For i = 1978 To 2021
                    ListBox1.Items.Add("-----------------------------------------")
                    ListBox1.Items.Add(CStr(i) & " - " & dc.GetDaysInYear(i))
    
                    For y = 1 To 12
                        Dim monthName = formatInfo.GetMonthName(y)
                        sw.WriteLine(CStr(i) & " " & dc.GetDaysInMonth(i, y) & " " & monthName)
                        'show the result in a Listbox
                        ListBox1.Items.Add(CStr(i) & " - " & dc.GetDaysInMonth(i, y) & " - " & monthName)
    
                    Next
                Next
                sw.Close()
            End Using
        End Sub
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  14. #14

    Thread Starter
    Junior Member
    Join Date
    Feb 2021
    Location
    Greece
    Posts
    17

    Re: Calculate Age-February issue

    Quote Originally Posted by ChrisE View Post
    did you try and use the GregorianCalendar ?

    see if you can use this
    Code:
     Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            Dim dc As New System.Globalization.GregorianCalendar()
            Dim formatInfo = System.Globalization.DateTimeFormatInfo.CurrentInfo
            ListBox1.Items.Add("Year" & "-" & "Days " & "-" & "Month ")
            ListBox1.Items.Add("-----------------------------------------")
    
            'write the result to a Textfile
            Using sw As New IO.StreamWriter("E:\TestFolder\TestfileYear_Days.txt")
                For i = 1978 To 2021
                    ListBox1.Items.Add("-----------------------------------------")
                    ListBox1.Items.Add(CStr(i) & " - " & dc.GetDaysInYear(i))
    
                    For y = 1 To 12
                        Dim monthName = formatInfo.GetMonthName(y)
                        sw.WriteLine(CStr(i) & " " & dc.GetDaysInMonth(i, y) & " " & monthName)
                        'show the result in a Listbox
                        ListBox1.Items.Add(CStr(i) & " - " & dc.GetDaysInMonth(i, y) & " - " & monthName)
    
                    Next
                Next
                sw.Close()
            End Using
        End Sub
    Hello, since it is the 1st time i get involved with VB.NET, i use the following method to calculate the dates.

    For example suppose i have my birth date (23rd November 1978)

    2021 2 26 (today's date)

    1978 11 23 (my date)


    So at days:26-23=3
    Months 2-11 is less that 0 so i subtract one year (2020) and add 12 months to February so it is now 14. So 14-11=3
    Years:2020(because we subtracted on year above)-1978=42

    So the result is 42 years , 3 months and 3 days. (Since i am new to VB.NET, i haven't figured out if the function is able to return an array for example, in which values 42,3,3 are stored and so on. So i return only the number 42, in this case.


    That's how i calculate with the above function. I've tested over several cases in Windows Calculator.

    I have read some theories over the net about the difficulties in date calculation, and what conventions run. For example , how to calculate the age from Feb. 28, 2015 to Mar. 31, of the X year? Suppose Feb. 28 to Mar. 31 as one month, then the result is one month and 3 days, but if we assume that February 28 is the end of the month and March 31 l, is end of the month as well, then the difference is one month. I used the first option in my function, and i compared the results with many sites regarding date calculation. Eventually i will modify my function to accept 2 dates as parameter and not only, calculate the difference based on today's date.

    I will check your own method as well to see what it looks like. Thanks for your suggestion.

  15. #15
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    2,725

    Re: Calculate Age-February issue

    this is one way to calculate

    Code:
    Option Strict On
    
    Public Class Form1
    
        Public Structure tDateDiff
            Dim Years As Integer
            Dim Months As Integer
            Dim Days As Integer
        End Structure
    
        Public Function GetDateDiff(ByVal vDateBegin As Date, _
                          ByVal vDateEnd As Date) As tDateDiff
            Dim vTemp As Date
            vDateBegin = CDate(vDateBegin)
            vDateEnd = CDate(vDateEnd)
            With GetDateDiff
                .Years = 0
                .Months = 0
                .Days = 0
                .Months = CInt(DateDiff("m", vDateBegin, vDateEnd))
                .Years = CInt(Int(.Months / 12))
                .Months = .Months - (.Years * 12)
                vTemp = DateAdd("yyyy", .Years, vDateBegin)
                vTemp = DateAdd("m", .Months, vTemp)
                .Days = CInt(DateDiff("d", vTemp, vDateEnd))
                If .Days < 0 Then
                    .Months = .Months - 1
                    vTemp = DateAdd("yyyy", .Years, vDateBegin)
                    vTemp = DateAdd("m", .Months, vTemp)
                    .Days = CInt(DateDiff("d", vTemp, vDateEnd))
                End If
                If .Months < 0 Then
                    .Months = 11
                    .Years = .Years - 1
                End If
    
    
            End With
        End Function
    
    
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            Dim vDateBegin As Date
            Dim vDateEnd As Date
            Dim tDiff As tDateDiff
            vDateBegin = CDate("23.11.1978 12:59:59") 'enter Birthdate
            vDateEnd = Now
            tDiff = GetDateDiff(vDateBegin, vDateEnd)
    
            With tDiff
                Label1.Text = CStr(.Years) & " :Years" & " " & CStr(.Months) & " :Months" & " " & CStr(.Days) & " :Days"
            End With
    
        End Sub
    End Class
    
    'Debug.Print is = 42:Years 3:Months 3:Days
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  16. #16
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    33,285

    Re: Calculate Age-February issue

    Thing is... your explanation of the math doesn't match the code ... also I don't see any accounting for Feb in the code either... but that's beside the point. If what you've written as the explanation is what you want, and is the expected result... start with that...

    pseudo code:
    Code:
    years = curryear - startyear
    months = currmonth - startmonth
    days = currdays - startdays
    
    // deal with negative days by adding 28/29/30/31 days as needed; then subtract one month
    if days < 0 then
      days += daysinMmonth(currentMonth)
      months -= 1
    
    // deal with negative months by adding 12; then subtracting a year
    if months <-0
      years -= 1
    
    // return the results
    -tg
    \
    \

    edit: all this said, I'm assuming this is just for education and fun... in reality, the start end dates could be Date variable and you can subtract one from the other which would give you a DateTimeSpan (I think that's right, might be just called a TimeSpan, maybe DateSpan? it's been a while) object from which you can get .Year, .Months, .Days, .TotalMonths, .TotalDays, as well as other properties...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  17. #17

    Thread Starter
    Junior Member
    Join Date
    Feb 2021
    Location
    Greece
    Posts
    17

    Re: Calculate Age-February issue

    Quote Originally Posted by techgnome View Post
    Thing is... your explanation of the math doesn't match the code ... also I don't see any accounting for Feb in the code either... but that's beside the point. If what you've written as the explanation is what you want, and is the expected result... start with that...

    pseudo code:
    Code:
    years = curryear - startyear
    months = currmonth - startmonth
    days = currdays - startdays
    
    // deal with negative days by adding 28/29/30/31 days as needed; then subtract one month
    if days < 0 then
      days += daysinMmonth(currentMonth)
      months -= 1
    
    // deal with negative months by adding 12; then subtracting a year
    if months <-0
      years -= 1
    
    // return the results
    -tg
    \
    \

    edit: all this said, I'm assuming this is just for education and fun... in reality, the start end dates could be Date variable and you can subtract one from the other which would give you a DateTimeSpan (I think that's right, might be just called a TimeSpan, maybe DateSpan? it's been a while) object from which you can get .Year, .Months, .Days, .TotalMonths, .TotalDays, as well as other properties...

    -tg
    Hello. Thanks for your answer. Since i am learning VB.NET, coming from much more older programming languages where there was not built-in "date" data type and methods or something thus i pass the parameters of year month, day as integer. As far it concerns February, I've read some articles about how to calculate dates, as I've post in previous post in this thread. This function in this version should be called "Age(1978,11,23) based on the example at previous posts. I am not aware of if i can return an Array or Dictionary or any other Data type with 3 values in it just like 42,3,3 in this example (with today's date is February 26th 2021).

    Since i now get involved in VB.NET i prefer to start create my own "tools" for future use, and improve them as long as my knowledge is getting improved. There are some pros and cons for my implementation, apart from the slight code improvements needed, because i literally created this "on the fly". Yes it works, after i tested it with several web platforms comparing the results and yes, this code could be more "VB.NETistic". I have already consider your suggestions for the above methods; i have already used some of them i.e. "DaysInMonth". I've already thought a revised version. Thanks again for your suggestion.
    Last edited by niktsol; Feb 26th, 2021 at 09:22 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width