Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

How to get 3-letter month abbreviations ?

by powerhouse (Friar)
on Feb 14, 2003 at 22:39 UTC ( [id://235436]=perlquestion: print w/replies, xml ) Need Help??

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

What function do I use to get the date in this format:
  • jan
  • feb
  • mar
  • apr
  • may
  • jun
  • jul
  • aug
  • sep
  • oct
  • nov
  • dec

I just want the months to look like that, for instance this month would obviously be feb

Or where do I look to find that, I don't exepect you to do it for me, I just cannot find it at perldoc.com


thx,
Richard

Title edit by tye

Replies are listed 'Best First'.
Re: Localtime
by Coruscate (Sexton) on Feb 14, 2003 at 22:43 UTC

    my ($month) = localtime =~ m#^[^ ]+ (\w{3})#; print $month, "\n";


    If the above content is missing any vital points or you feel that any of the information is misleading, incorrect or irrelevant, please feel free to downvote the post. At the same time, reply to this node or /msg me to tell me what is wrong with the post, so that I may update the node to the best of my ability. If you do not inform me as to why the post deserved a downvote, your vote does not have any significance and will be disregarded.

      or
      my $month = lc((split ' ',localtime)[1]);
      or
      my @months = qw( jan feb mar apr may jun jul aug sep oct nov dec ) +; my $month = $months[ (localtime)[4] ];

      jdporter
      The 6th Rule of Perl Club is -- There is no Rule #6.

      Absolutely. In scalar context,

      print scalar(localtime), "\n";

      localtime returns a time string like:

      Fri Feb 14 14:53:08 2003

      I never actually realized it until you pointed it out. I always did it the hard way and constructed an array of month names.

      Thanks coruscate++

      Where do you want *them* to go today?
      Awesome, thank you! One more question, please.

      What I'm trying to do, is get the month, then I'm going to build a popup_menu using CGI.pm that has that months days in it as the values they can select.

      However, know I realize that there must be a module that I might be able to find that already does that, otherwise I'd have to create 12 hashes, one for each month, then call that, depending on the value of the month.

      Ex: jan has 31 days, so the days popup_menu would have 1 - 31 to be selected from. feb has 28, possibly 29... etc.

      Is there a module that does that.

      Sorry for the trouble. I should have realized that 15 mins ago, before I asked the other question.

      Thank you for any module you can think of for me. I am not good with knowing what modules are and so forth.

      thx,
      Richard
        The module Calendar::Simple will do it. Here is how you could use it to get the days in the current month (it gets the last day of the last week of the month):
        use strict; use Calendar::Simple; my $mon = (localtime)[4] + 1; my $yr = ((localtime)[5] + 1900); my @month = calendar($mon, $yr); print $month[-1][-1],"\n";
Re: Localtime
by Abigail-II (Bishop) on Feb 15, 2003 at 01:11 UTC
    I would use strftime from the POSIX module. That gives you the ability to format your date/time in lots and lots of ways.
    $ perl -MPOSIX -wle 'print strftime "%b" => localtime' Feb

    Abigail

Re: Localtime
by steves (Curate) on Feb 15, 2003 at 11:14 UTC

    I didn't realize until investigating this thread that the scalar return of localtime is a built-in and is not locale dependent. I figured it was like UNIX ctime so I cringed at some of the scalar solutions since I thought they'd break if the default time locale setting changed. But I was wrong.

    Before realizing this, my first choice would have been to use the POSIX strftime method as proposed by Abigail-II. Another way is this:
    $mon = (qw/jan feb mar apr may jun jul aug sep oct nov dec/)[(localtim +e)[4]]; print "$mon\n";
Re: Localtime
by fredopalus (Friar) on Feb 14, 2003 at 23:18 UTC
    Try
    $month = lc(substr(scalar localtime, 4, 4));
    You may also wan't to look at localtime.
A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2024-03-29 10:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found