perl - 格式化日期时间输出
如何使用 DateTime 从以下位置转换我的退货:
This is my date:2011-11-26T20:11:06
到 这是我的日期:20111126
使用此现有代码:
use DateTime qw();
my $dt3 = DateTime->now->subtract(days => 1);
print "This is my date:$dt3\n"
How do I convert my return using DateTime from:
This is my date:2011-11-26T20:11:06
toThis 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"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
ymd
是最简单的:strftime
是更通用的:也有特定的(例如 DateTime::Format::Atom)和一般(例如 DateTime::Format::Strptime) 您可以使用的格式化辅助工具:
PS — 您的代码将给出英格兰境内或附近的日期,而不是您所在位置的日期。为此,您想要
或更合适的
ymd
is the simplest:strftime
is more general purpose:There are also specific (e.g. DateTime::Format::Atom) and general (e.g. DateTime::Format::Strptime) formatting helper tools you can use:
PS — Your code will give the date in or near England, not the date where you are located. For that, you want
or the more appropriate
只需在第二行添加
->ymd("")
。参数""
是分隔符,您选择为空字符串。Just add
->ymd("")
on the second line. The parameter""
is the separator, which you chose to be an empty string.在 Perl 中大约有十几种处理日期的方法。但是,如果您知道日期字符串的格式,则可能没有理由调用日期时间模块:
我不熟悉 DateTime,但如果在显示时没有办法格式化数据,我会感到惊讶它。
我个人更喜欢 Time::Piece 和 Time::Seconds 用于这些事情。自 5.10 起,这些模块就成为标准 Perl 安装的一部分。另外,我发现界面相当简单干净。
由于某种原因,您不能在同一行上说
$time = localtime - ONE_DAY;
。我想您需要先创建Time::Piece
对象,然后才能使用Time::Second
常量操作它们。There are about a dozen ways to process dates in Perl. However, if you know the format of the date string, there maybe no reason to call a datetime module:
I'm not familiar with DateTime, but I'd be surprised if there wasn't a way to format the data when you display it.
I personally prefer Time::Piece and Time::Seconds for these things. These modules have been part of the standard Perl installation since 5.10. Plus, I find the interface to be fairly simple and clean.
For some reason, you can't say
$time = localtime - ONE_DAY;
on the same line. I guess you need to create theTime::Piece
object first before you can manipulating them with theTime::Second
constants.