开发者

Perl date function/module able to understand full unabbreviated months

Let's say I read in a string from somewhere that contains a date, 开发者_运维知识库and it's date format doesn't abbreviate the month. Is there a module that can handle reading it in, and then outputting it to whichever format I choose? I've taken a quick look through CPAN, and every date module I looked at didn't seem to accommodate an unabbreviated month.

Thanks for any help

EDIT: As an example, say we have a string like this; "2 February 1988". Now we want to convert it into "1988-02-02" (YYYY-MM-DD).


You can utilize core Time::Piece module and its strptime method. The format is described on strftime man page, for full month name there is %B format specifier:

use Time::Piece;
my $dt = Time::Piece->strptime("2 February 1988", "%d %B %Y");
print $dt->ymd,"\n";


Using DateTimeX::Easy:

Program

my $dt = DateTimeX::Easy->new('2 February 1988');
print $dt->date();

Output

1988-02-02


I always use Date::Parse, since it can automatically detect the input format and convert it to unix time with "str2time". If I then want to format the date I use "strftime".

use Date::Parse;
use POSIX 'strftime';

$unix_time = str2time "2 February 1988";
print strftime "%Y-%m-%d", localtime $unix_time;


You should write your own conversion module.

You can use hashes to do quick conversions like this:

my %month = ("Jan" => "01", "Feb" => "02", "Mar" => "03", "Apr" => "04", "May" => "05", "Jun" => "06", "Jul" => "07", "Aug" => "08", "Sep" => "09", "Oct" => "10", "Nov" => "11", "Dec" => "12");

my %day = ("Sun" => "01", "Mon" => "02", "Tue" => "03", "Wen" => "04", "Thu" => "05", "Fri" => "06", "Sat" => "07");

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜