我的XML日期输入是:2012-12-21即YYYY-MM-DD格式。 我想将月份数转换为月份名称。即2012年12月21日 任何人都可以建议哪个perl模块是有用的&如何使用它作为一个例子,因为我对perl很新。
答案 0 :(得分:10)
使用核心perl的Time::Piece
:
#!/usr/bin/perl
use 5.10;
use strict;
use warnings;
use Time::Piece;
my $date = '2012-12-21';
# strftime format - http://www.unix.com/man-page/FreeBSD/3/strftime/
my $t = Time::Piece->strptime($date, '%Y-%m-%d');
say $t->month; # Dec
say $t->strftime('%Y-%b-%d'); # 2012-Dec-21
答案 1 :(得分:3)
use strict;
my $date='2012-12-21';
my %mons = ("01"=>'Jan',"02"=>'Feb',"03"=>'Mar',"04"=>'Apr',"05"=>'May',"06"=>'Jun',"07"=>'Jul',"08"=>'Aug',"09"=>'Sep',"10"=>'Oct',"11"=>'Nov',"12"=>'Dec');
$date=~s/(\d{4})\-(\d{2})\-(\d{2})/my $year=$1; my $month=$2; my $day=$3; "$year-$mons{$month}-$day"/egs;
print $date;