Reputation: 28349
I'm curious if there's a "best practices" approach to doing age verification. I was thinking I would grab the milliseconds from right now and the milliseconds from their birthdate and then see if their birthdate + ageReq * (365 * millPerYear) > currentMillisecond.
I believe this will work, but am wondering if there's either anything faulty with this approach and/or if there's a library that tackles whatever pitfalls I may not be anticipating here (for example, is there a leap year problem?)
This problem is language agnostic, I'm currently in Java, but am curious what the right way to do this would be irrespective of language.
TIA
Upvotes: 2
Views: 787
Reputation: 9590
You can try joda-time. And yes there is slight flaw in your check. This is not very accurate. Depending upon, how much legal compliance you have to follow, you can either live with it or not. Joda time is a good library as it does take care of all basic checks.
If you are looking for language agnostic. Then you can look into the code of Joda-time. as it precisely calculates the dates.
Upvotes: 7
Reputation: 8823
If you are using an existing birthdate then use the Date and Calendar classes to get an accurate difference. If you use your method then it isnt exact because of leap years / seconds etc... Dates are a mess to work out by hand.
If you dont currently have a birthdate and you dont need to record it I would advise against your application actually asking for one, because its annoying as hell, and when I have to do it I just scroll down the years and pick a random one older than 20 or so. Just ask yes or no if you are over/under X.
Upvotes: 2