From perldoc -f localtime,
and# 0 1 2 3 4 5 6 7 8 ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$mday is the day of the month and $mon the month in the range 0..11, with 0 indicating January and 11 indicating December. This makes it easy to get a month name from a list:Given this, then:$year contains the number of years since 1900. To get a 4-digit year write:my @abbr = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec); print "$abbr[$mon] $mday"; # $mon=9, $mday=18 gives "Oct 18"$year += 1900;
or# 2014-02 perl -le 'my (@ym) = (localtime($^T))[5,4]; $ym[0] += 1900; $ym[1]++; print sprintf qq{%4d-%02d}, @ym;'
# 2014-Feb perl -le 'my @abbr = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec); my (@ym) = (localtime)[5,4]; $ym[0]+= 1900; print sprintf qq{%4d-%3s}, $ym[0], $abbr[$ym[1]];'
Hope that helps.
In reply to Re: extract month and year from localtime
by atcroft
in thread extract month and year from localtime
by oikool
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |