Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Fetching Only Month and Year From localtime()

by amt (Monk)
on Jan 05, 2005 at 16:53 UTC ( #419656=perlquestion: print w/ replies, xml ) Need Help??
amt has asked for the wisdom of the Perl Monks concerning the following question:

Gentlemen,
I'm looking to grab the current month and current year to default in a form, but I don't want to create an array. Can I access the localtime elements directly with localtime[0] or is there another method.

Update: Many thanks. As Juerd mentioned, I was trying reform the date to the mysql format. I guess I could have just used POSIX::strtime, as mentioned in perlcheat, which is in my sig. :/
amt.

perlcheat

Comment on Fetching Only Month and Year From localtime()
Select or Download Code
Re: localtime
by gellyfish (Monsignor) on Jan 05, 2005 at 16:55 UTC

    I think you'll probably want something like:

    my ($month, $year) = (localtime())[4,5];
    You may want to fix the offsets afterwards of course.

    /J\

Re: localtime
by jZed (Prior) on Jan 05, 2005 at 16:56 UTC
    Either "print scalar localtime" for a pre-formatted string, or else grab the elements of the localtime array and format the string yourself (not forgetting about things like 0-based months).
Re: localtime
by ikegami (Pope) on Jan 05, 2005 at 16:56 UTC
Re: localtime
by ysth (Canon) on Jan 05, 2005 at 20:11 UTC
    The (LIST)[LIST] syntax others have shown is called a list slice. It allows you to have an arbitrary expression that generates a list in the () and extract arbitrary elements from it, by specifying the index/indicies in the [].
Re: localtime
by ambrus (Abbot) on Jan 05, 2005 at 20:28 UTC

    Either

    $year = 1900 + (localtime)[5]; $mon = 1 + (localtime)[4];
    or
    (undef, undef, undef, undef, $mon, $year) = localtime; $mon += 1;
    or, combining the two
    ($mon, $year) = (localtime)[4,5]; $mon += 1; $year += 1900; warn $year +, " ", $mon;

    In all these cases, the parentheses do the magic: in the first case, (LIST)[INDICES] the parenthesis is part of the syntax of the indexing operator. In the second case, the parentheses make the assignment a list assignment.

    It's because all this magic (and the magic of the x operator), that some people think parenthesis are the operator for creating lists in perl.

    Just one more thing about parens: qw/jan feb mar apr may jun jul aug sep oct nov dec/[(localtime)[4]] returns the abbrev of the current month (don't use this, as it does not take locales into account), and it does indexing without parenthesis or a silgil ($ or @), so it contradicts to what I've said.

Re: Fetching Only Month and Year From localtime()
by Juerd (Abbot) on Jan 05, 2005 at 23:01 UTC

    I'm looking to grab the current month and current year to default in a form, but I don't want to create an array. Can I access the localtime elements directly with localtime[0] or is there another method.

    In addition to the list slices and list assignments with undef on the LHS, you can also opt to use POSIX::strftime. This also takes the way the need to add 1 to the month and 1900 to the year. As I don't see the point in having separate variables for this, I guess you're building an ISO date in "2005-01" format.

    use POSIX qw(strftime); my $month = strftime "%Y-%m", localtime;

    Juerd # { site => 'juerd.nl', plp_site => 'plp.juerd.nl', do_not_use => 'spamtrap' }

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2014-09-01 21:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (17 votes), past polls