How To Get a Formatted Date in Perl

1 2 3 |
use POSIX qw(strftime); my $stamp = strftime "%Y%m%d%H%M%S", localtime; my $stamp = strftime "%Y%m%d%H%M%S", gmtime; |
![]() |
1 2 3 |
use POSIX qw(strftime); my $stamp = strftime "%Y%m%d%H%M%S", localtime; my $stamp = strftime "%Y%m%d%H%M%S", gmtime; |
This simple formula will give a Y2K-compliant 4-digit year with low overhead:
1 |
$year = (localtime)[5] + 1900; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
use Date::Manip; my @months = qw/Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec/; my $month = $months[ (localtime)[4] ]; my $last = &UnixDate("last saturday in $month", "%B %e"); $last .= &gensuffix($last); sub gensuffix { my($val) = @_; my $end_num = substr($val, -1, 1); my $suffix = 'th'; $suffix = 'st' if $end_num == 1; $suffix = 'nd' if $end_num == 2; $suffix = 'rd' if $end_num == 3; return $suffix; } |