如何在Perl中更改日期时间格式?
我想将日期时间格式转换为我想要的格式 如何将日期时间格式从2014年11月21日星期五15:04:32转换为2014-11-21 15:04:32 谢谢 尝试一下这个粗糙的方法,但它能满足您的需求。使用date::manip灵活使用多年来一直是Perl的核心 使用如何在Perl中更改日期时间格式?,perl,Perl,我想将日期时间格式转换为我想要的格式 如何将日期时间格式从2014年11月21日星期五15:04:32转换为2014-11-21 15:04:32 谢谢 尝试一下这个粗糙的方法,但它能满足您的需求。使用date::manip灵活使用多年来一直是Perl的核心 使用strptime(字符串解析时间)解析日期/时间字符串 使用strftime(字符串格式时间)根据需要格式化日期/时间 #!/usr/bin/perl use strict; use warnings; use 5.010; use
strptime
(字符串解析时间)解析日期/时间字符串
使用strftime
(字符串格式时间)根据需要格式化日期/时间
#!/usr/bin/perl
use strict;
use warnings;
use 5.010;
use Time::Piece;
# Formats are defined in "man strftime"
my $in_fmt = '%a %b %d %Y %H:%M:%S';
my $out_fmt = '%Y-%m-%d %H:%M:%S';
my $in_date = 'Fri Nov 21 2014 15:04:32';
my $date = Time::Piece->strptime($in_date, $in_fmt);
my $out_date = $date->strftime($out_fmt);
say $out_date;
您想使用和日期模块,还是想在纯perl中执行?如何在纯perl中执行?
POSIX::strftime()
在核心中,是“纯”吗?如何使用strftime()要将datetime格式从2014年11月21日星期五15:04:32转换为2014-11-21 15:04:32?或使用其他方法将datetime格式从2014年11月21日星期五15:04:32转换为2014-11-21 15:04:32?Date::Manip绝对不是一个好建议。Time::Piece自2007年以来一直是core Perl的一部分。感谢Dave,我很久以前就使用了Date::Manip,无论Time::Piece的外观如何,再次感谢您的建议。任何一个开始需要构建自己的哈希或月名列表的解决方案都将非常糟糕。有一些函数可以进行这种解析——在本例中是strtime
。如果您维护自己的哈希,它将无法正确本地化。
#!/usr/bin/perl
use strict;
use warnings;
use 5.010;
use Time::Piece;
# Formats are defined in "man strftime"
my $in_fmt = '%a %b %d %Y %H:%M:%S';
my $out_fmt = '%Y-%m-%d %H:%M:%S';
my $in_date = 'Fri Nov 21 2014 15:04:32';
my $date = Time::Piece->strptime($in_date, $in_fmt);
my $out_date = $date->strftime($out_fmt);
say $out_date;