perl格式化DateTime输出 [英] perl - formatting DateTime output
本文介绍了perl格式化DateTime输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的日期:2011-11-26T20:11:06
到
这是我的日期:20111126
使用此现有代码:
使用DateTime qw();
我的$ dt3 = DateTime-> now-> subtract(days => 1);
print这是我的日期:$ dt3\\\
解决方案
ymd
是最简单的:
print这是我的日期:,$ dt3-> ymd(''),\\\
;
strftime
更通用: p>
print这是我的日期:,$ dt3-> strftime('%Y%m%d'),\\ \\ n;
还有具体的(例如 DateTime :: Format :: Atom )和一般(例如 DateTime :: Format :: Strptime )格式化助手工具,您可以使用:
使用DateTime :: Format :: Strptime qw();
my $ format = DateTime :: Format :: Strptime-> new(pattern =>'%Y%m%d');
print这是我的日期:,$ format-> format_datetime($ dt3),\\\
;
PS—您的代码将在英国或其附近的日期,而不是您所在的日期。为此,您需要
my $ dt3 = DateTime-> now(time_zone =>'local');
或更合适的
my $ dt3 = DateTime-> today(time_zone =>'local');
How do I convert my return using DateTime from:
This is my date:2011-11-26T20:11:06
to
This is my date:20111126
Using this existing code:
use DateTime qw();
my $dt3 = DateTime->now->subtract(days => 1);
print "This is my date:$dt3\n"
解决方案
ymd
is the simplest:
print "This is my date: ", $dt3->ymd(''), "\n";
strftime
is more general purpose:
print "This is my date: ", $dt3->strftime('%Y%m%d'), "\n";
There are also specific (e.g. DateTime::Format::Atom) and general (e.g. DateTime::Format::Strptime) formatting helper tools you can use:
use DateTime::Format::Strptime qw( );
my $format = DateTime::Format::Strptime->new( pattern => '%Y%m%d' );
print "This is my date: ", $format->format_datetime($dt3), "\n";
PS — Your code will give the date in or near England, not the date where you are located. For that, you want
my $dt3 = DateTime->now(time_zone => 'local');
or the more appropriate
my $dt3 = DateTime->today(time_zone => 'local');
这篇关于perl格式化DateTime输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文