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

How to get 3-letter month abbreviations ?

by powerhouse (Friar)
on Feb 14, 2003 at 22:39 UTC ( #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


Title edit by tye

  • Comment on How to get 3-letter month abbreviations ?

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.

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

      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 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.

        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


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
    $month = lc(substr(scalar localtime, 4, 4));
    You may also wan't to look at localtime.
Re: Localtime
by BUU (Prior) on Feb 14, 2003 at 23:06 UTC
      Ah, I've been to both. I don't have no clue what I'm searching for. I've tried searching for "months" and "days", but they both pull up TONS of things, and I don't have the time to go through
      "2061 Found" for 'days of month',
      "1089 Found" for 'days',
      "399 Found" for 'months'.

      I did go through the first few of 'months' but they are so far off, from what I'm looking for, I think it would waste my time to go through all the rest, as I'm not learning from them, just skimming through, looking for what I need. If I were learning stuff, that would be another story, but I'm not.

      So that is why I asked, since someone may already be using it, and can tell me what I'm looking for.

      Then I can go find it and learn from that one.

      Even, if I knew what It is called(what to search for by name) then I can do it. thx,
        Thank you! I guess I got my answer. I'm getting the module now, and going to give it a shot.

        Many thanks!!


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://235436]
Approved by Coruscate
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2018-03-18 02:47 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (228 votes). Check out past polls.