如何使用DateTime转换我的回报:
This is my date:2011-11-26T20:11:06
至
This is my date:20111126
使用此现有代码:
use DateTime qw();
my $dt3 = DateTime->now->subtract(days => 1);
print "This is my date:$dt3\n"
Run Code Online (Sandbox Code Playgroud)
ike*_*ami 11
ymd 是最简单的:
print "This is my date: ", $dt3->ymd(''), "\n";
Run Code Online (Sandbox Code Playgroud)
strftime 更通用的目的:
print "This is my date: ", $dt3->strftime('%Y%m%d'), "\n";
Run Code Online (Sandbox Code Playgroud)
您还可以使用特定的(例如DateTime :: Format :: Atom)和一般(例如DateTime :: Format :: Strptime)格式化帮助工具:
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";
Run Code Online (Sandbox Code Playgroud)
PS - 您的代码将在英格兰或其附近提供日期,而不是您所在的日期.为此,你想要
my $dt3 = DateTime->now(time_zone => 'local');
Run Code Online (Sandbox Code Playgroud)
或者更合适
my $dt3 = DateTime->today(time_zone => 'local');
Run Code Online (Sandbox Code Playgroud)
只需->ymd("")在第二行添加即可.参数""是分隔符,您选择将其作为空字符串.
use DateTime qw();
my $dt3 = DateTime->now->subtract(days => 1)->ymd("");
print "This is my date:$dt3\n"
Run Code Online (Sandbox Code Playgroud)