calculation of age

Status
Not open for further replies.

Rhayezelle

Member
Joined
Aug 14, 2010
Messages
24
Location
Philippines
Programming Experience
Beginner
could you help me how to compute an exact age of a person using the format date of mm/dd/yy... in vb.net, i was trying to code it but the result was not the exact age of the person.. please help me... :(
 
You can use many different methods for finding the age. for instance you can use TimeSpan Structure (System)
VB.NET:
        Dim birthdate As DateTime = DateTime.Parse("05/13/1975")
        Dim interval As TimeSpan = DateTime.Now - birthdate
        Dim years As Integer = interval.Days / 365
        Dim days As Integer = interval.Days Mod 365
        Console.WriteLine("This person is old " & years & " years and " & days & " days")
 
You can use many different methods for finding the age. for instance you can use TimeSpan Structure (System)
VB.NET:
        Dim birthdate As DateTime = DateTime.Parse("05/13/1975")
        Dim interval As TimeSpan = DateTime.Now - birthdate
        Dim years As Integer = interval.Days / 365
        Dim days As Integer = interval.Days Mod 365
        Console.WriteLine("This person is old " & years & " years and " & days & " days")

You're going to want to floor that division there, because if their birthday is between 6-12 months ago casting to int will cause that to round up making them appear to be 1 year older then they actually are...

Even more so though, that won't assist in dealing with things like leap year. This method will have 1 day error for every 4 years of progression, but you can't just adjust by that because we only adjust the day every four years, so the adjustment would have to be based on the starting year, and the ending year (current year).

The easier way would to NOT use TimeSpan all together. But instead resolve the age the same way we treat birthdays in society. That is we take the difference of birth year, then check if today is BEFORE or AFTER the birthday.

like so:

VB.NET:
        Dim bday As DateTime = DateTime.Parse("10/01/1983")
        Dim today As DateTime = DateTime.Now

        Dim years As Integer = today.Year - bday.Year ''take the difference in year
        Dim days As Integer = today.DayOfYear - bday.DayOfYear ''get the difference in days

''If today is before the birthday then we subtract 1 from the years and add that year (365) to the days
        If days < 0 Then
            years -= 1
            days += 365
        End If

        Console.WriteLine("You are {0} year/s and {1} day/s old", years, days)




Give you an example of how the TimeSpan would really suck. On my buddies 21st bday he went to the packy to get his first legally purchased case of beer, the software the store used to check dates obviously used an improper algorithm to compensate for the "leap year" and it said he wasn't 21 for whole 24 hours. The license said the same day as the date it was on the calendar, and the guy behind the register was confused... but the register was one of those registers that wouldn't complete sale if the supplied license didn't pass its algorithm. So my buddy didn't get his booze... despite the fact that he WAS 21.

Of course one could say "oh well, you can't do it on the birthday... whatever"

Which is what a lot of people said to me when I told them the story. But hey, all it really required was to apply the same algorithm to the problem that we based the law off of.
 
Last edited:
I wasn't try to call anyone out here...

I was making an anecdote to describe how one may want to consider approaching problem solving. If it's a problem that already exists and is solved in the real world, you should solve the problem the same way as the real world... otherwise the code may confuse and irritate users.


Where I went to school we didn't except a teacher saying "we do it this way because we say so." All that received in return is a classroom of "but whyyyyyyy?"
 
Ok as you wish but, this is a very basic code actually. Thats why i told you that you overreacted in your previous post. It sounds like a too much talking about pretty nothing.
I just wrote some code off the top of my head and then you replied offering a better solution. Don't you think it could be enough?
 
yes


I will speak only few words



this will be best



come on dude, it's a forum. Some of us write quick turse posts, others write long winded posts. It's just the way we each write...

If you don't want to deal with my post because it's just to much. Then don't read it.

What's worst is when someone wants to send a thread off topic... oh wait, is that what just happened? I know I know, low jab, but hopefully you get my point.
 
Last edited:
thank you guys

just cool it guys... thank you very much for your answers regarding to my problem... I deeply apologize for my topic that has cause both of you of misunderstanding each others opinion.... :(, ...


Thank you again for helping me in my problem...:D
 
It is not about the opinions Rhayezelle.

This guy seems to be good at telling stories. But who of you come here for reading somone's stories?

I am pretty certain that people come here for help and nothing but help. Period!

He made it to sound very theatrical but it's not. Especially because of the fact that it can be easily found on Internet. Click here and see for yourself Let me google that for you

In addition i hate when someone search for a pre-written solution or partial solution and present it like a own code. Copy and paste programming - Wikipedia, the free encyclopedia

Does this seems familiar to you? http://www.developerfusion.com/code/4671/calculate-age/ (By James Crowley, published on 20 Feb 2005). It looks like a very good starting point indeed

However it seems that he is right about one thing:

lordofduct said:
If you don't want to deal with my post because it's just to much. Then don't read it.
 
It is not about the opinions Rhayezelle.

This guy seems to be good at telling stories. But who of you come here for reading somone's stories?

I am pretty certain that people come here for help and nothing but help. Period!

He made it to sound very theatrical but it's not. Especially because of the fact that it can be easily found on Internet. Click here and see for yourself Let me google that for you

In addition i hate when someone search for a pre-written solution or partial solution and present it like a own code. Copy and paste programming - Wikipedia, the free encyclopedia

Does this seems familiar to you? http://www.developerfusion.com/code/4671/calculate-age/ (By James Crowley, published on 20 Feb 2005). It looks like a very good starting point indeed

However it seems that he is right about one thing:

wow dude,

you accuse me of plagiarizing code? Screw you!

When considering a simple algorithm, like the one proposed. And I resolve it the way EVERYONE in the real world does (my point in my "theatrical" anecdote)... yeah of course there's going to be similarities. It's only a few lines of code...

What do you think I was trying to call you out for your post? No I wasn't, I wasn't trying to piss you off or anything.

I had a response, with a more correct answer, and I happened to have an anecdote in it... and you just got a craw up your ass about it. Piss off.


Did I ever accuse you of anything as horrible as plagiarism? What did I do? I pointed out some flaws in your code snippet, and then made a suggestion to anyone reading that when approaching solving a real world problem, you aught to solve the way it's solved in the real world. Is that such a big deal? Is that really so annoying that you have to accuse some of plagiarism?



now that, that's me getting pissed off and emotional. Now I aught to "cool it"...
 
Last edited:
Ok, time to end this stupid debate. Answering questions is what this forum is about, not complaining about how much detail a poster gives or not.
 
Status
Not open for further replies.
Back
Top