Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

return age

by bigup401 (Pilgrim)
on Sep 07, 2020 at 15:38 UTC ( [id://11121440]=perlquestion: print w/replies, xml ) Need Help??

bigup401 has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: return age
by marto (Cardinal) on Sep 07, 2020 at 15:51 UTC

    The logic of subtracting years to determine someone age is flawed, you can't just compare the years and expect to get the right answer.

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: return age
by AnomalousMonk (Archbishop) on Sep 07, 2020 at 17:26 UTC

    Calendars are lotsa fun, a rabbit hole down which you can lose yourself for... well, years. See the classic Calendrical Calculations by Reingold and Dershowitz.


    Give a man a fish:  <%-{-{-{-<

Re: return age
by LanX (Saint) on Sep 07, 2020 at 20:08 UTC
    DB<20> use DateTime DB<21> $now = DateTime->now; DB<22> $dt = DateTime->new( year => 1964, month => 10, day => 16) DB<23> $duration = $now - $dt DB<24> p $duration->years 55 # not 56! DB<25>

    see DateTime::Duration

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

Re: return age
by tobyink (Canon) on Sep 07, 2020 at 16:38 UTC

    Here's how I'd do it...

    #!/usr/bin/env perl use strict; use warnings; use DateTime; use DateTime::Format::Natural; my $date_of_birth; until ( defined $date_of_birth ) { print "When were you born?\n"; my $parsed = DateTime::Format::Natural->new->parse_datetime( ~~<> +); printf "Did you mean %s? (Type 'y' if yes.)\n", $parsed->strftime( + '%a, %d %b %Y' ); $date_of_birth = $parsed if <> =~ /y/i; } my $diff = DateTime->now - $date_of_birth; printf "You are %d years old.\n", $diff->years;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11121440]
Approved by jcb
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (2)
As of 2024-04-26 03:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found