5

My XML date input is : 2012-12-21 i.e. YYYY-MM-DD format. I want to convert month number into month name. i.e. 2012-Dec-21 Can anyone suggest which perl module is useful & how to use it with an example since I am very new to perl.

4

2 回答 2

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
于 2013-06-05T10:43:28.277 回答
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;
于 2013-06-05T12:22:07.567 回答