perl - 格式化日期时间输出

发布于 2024-12-18 07:15:09 字数 270 浏览 0 评论 0原文

如何使用 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
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"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

[旋木] 2024-12-25 07:15:09

ymd 是最简单的:

print "This is my date: ", $dt3->ymd(''), "\n";

strftime 是更通用的:

print "This is my date: ", $dt3->strftime('%Y%m%d'), "\n";

也有特定的(例如 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";

PS — 您的代码将给出英格兰境内或附近的日期,而不是您所在位置的日期。为此,您想要

my $dt3 = DateTime->now(time_zone => 'local');

或更合适的

my $dt3 = DateTime->today(time_zone => 'local');

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');
莳間冲淡了誓言ζ 2024-12-25 07:15:09

只需在第二行添加 ->ymd("") 。参数""是分隔符,您选择为空字符串。

use DateTime qw();
my $dt3 = DateTime->now->subtract(days => 1)->ymd("");
print "This is my date:$dt3\n"

Just add ->ymd("") on the second line. The parameter "" is the separator, which you chose to be an empty string.

use DateTime qw();
my $dt3 = DateTime->now->subtract(days => 1)->ymd("");
print "This is my date:$dt3\n"
朕就是辣么酷 2024-12-25 07:15:09

在 Perl 中大约有十几种处理日期的方法。但是,如果您知道日期字符串的格式,则可能没有理由调用日期时间模块:

$dt3 =~ /^(\d+)-(\d+)-(\d+)/;
print "This is my date:${1}${2}${3}\n";

我不熟悉 DateTime,但如果在显示时没有办法格式化数据,我会感到惊讶它。

我个人更喜欢 Time::PieceTime::Seconds 用于这些事情。自 5.10 起,这些模块就成为标准 Perl 安装的一部分。另外,我发现界面相当简单干净。

use Time::Piece;
use Time::Seconds;

my $time = localtime;
$time -= ONE_DAY;

print "This is my date:" . $time->ymd("");

由于某种原因,您不能在同一行上说 $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:

$dt3 =~ /^(\d+)-(\d+)-(\d+)/;
print "This is my date:${1}${2}${3}\n";

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.

use Time::Piece;
use Time::Seconds;

my $time = localtime;
$time -= ONE_DAY;

print "This is my date:" . $time->ymd("");

For some reason, you can't say $time = localtime - ONE_DAY; on the same line. I guess you need to create the Time::Piece object first before you can manipulating them with the Time::Second constants.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文