今天的日期在Perl的MM/DD/YYYY格式。

时间:2022-06-01 19:56:34

I'm working on a Perl program at work and stuck on (what I think is) a trivial problem. I simply need to build a string in the format '06/13/2012' (always 10 characters, so 0's for numbers less than 10).

我正在研究一个Perl程序,并坚持(我认为是)一个小问题。我只需要在格式“06/13/2012”中构建一个字符串(总是10个字符,所以0的数字小于10)。

Here's what I have so far:

以下是我目前所拥有的:

use Time::localtime;
$tm=localtime;
my ($day,$month,$year)=($tm->mday,$tm->month,$tm->year);

7 个解决方案

#1


53  

You can do it fast, only using one POSIX function. If you have bunch of tasks with dates, see the module DateTime.

你可以快速地做,只用一个POSIX函数。如果您有大量的日期任务,请参见模块DateTime。

use POSIX qw(strftime);

my $date = strftime "%m/%d/%Y", localtime;
print $date;

#2


51  

You can use Time::Piece, which shouldn't need installing as it is a core module and has been distributed with Perl 5 since version 10.

您可以使用Time::Piece,它不需要安装,因为它是一个核心模块,并且自版本10以来已经与Perl 5一起分发了。

use Time::Piece;

my $date = localtime->strftime('%m/%d/%Y');
print $date;

output

输出

06/13/2012


Update

You may prefer to use the dmy method, which takes a single parameter which is the separator to be used between the fields of the result, and avoids having to specify a full date/time format

您可能更喜欢使用dmy方法,该方法接受单个参数,该参数是在结果字段之间使用的分隔符,并避免指定完整的日期/时间格式。

my $date = localtime->dmy('/');

This produces an identical result to that of my original solution

这就产生了与我原来的解决方案相同的结果。

#3


12  

use DateTime qw();
DateTime->now->strftime('%m/%d/%Y')   

expression returns 06/13/2012

表达式返回06/13/2012

#4


8  

If you like doing things the hard way:

如果你喜欢做一些困难的事情:

my (undef,undef,undef,$mday,$mon,$year) = localtime;
$year = $year+1900;
$mon += 1;
if (length($mon)  == 1) {$mon = "0$mon";}
if (length($mday) == 1) {$mday = "0$mday";}
my $today = "$mon/$mday/$year";

#5


2  

use Time::Piece;
...
my $t = localtime;
print $t->mdy("/");# 02/29/2000

#6


1  

Formating numbers with leading zero is done easily with "sprintf", a built-in function in perl (documentation with: perldoc perlfunc)

用“sprintf”(perl中的一个内置函数)可以很容易地完成与前导零的格式化数字(文档:perldoc perlfunc)

use strict;
use warnings;
use Date::Calc qw();
my ($y, $m, $d) = Date::Calc::Today();
my $ddmmyyyy = sprintf '%02d.%02d.%d', $d, $m, $y;
print $ddmmyyyy . "\n";

This gives you:

这给你:

14.05.2014

14.05.2014

#7


0  

Perl Code for Unix systems:

Unix系统的Perl代码:

# Capture date from shell
my $current_date = `date +"%m/%d/%Y"`;

# Remove newline character
$current_date = substr($current_date,0,-1);

print $current_date, "\n";

#1


53  

You can do it fast, only using one POSIX function. If you have bunch of tasks with dates, see the module DateTime.

你可以快速地做,只用一个POSIX函数。如果您有大量的日期任务,请参见模块DateTime。

use POSIX qw(strftime);

my $date = strftime "%m/%d/%Y", localtime;
print $date;

#2


51  

You can use Time::Piece, which shouldn't need installing as it is a core module and has been distributed with Perl 5 since version 10.

您可以使用Time::Piece,它不需要安装,因为它是一个核心模块,并且自版本10以来已经与Perl 5一起分发了。

use Time::Piece;

my $date = localtime->strftime('%m/%d/%Y');
print $date;

output

输出

06/13/2012


Update

You may prefer to use the dmy method, which takes a single parameter which is the separator to be used between the fields of the result, and avoids having to specify a full date/time format

您可能更喜欢使用dmy方法,该方法接受单个参数,该参数是在结果字段之间使用的分隔符,并避免指定完整的日期/时间格式。

my $date = localtime->dmy('/');

This produces an identical result to that of my original solution

这就产生了与我原来的解决方案相同的结果。

#3


12  

use DateTime qw();
DateTime->now->strftime('%m/%d/%Y')   

expression returns 06/13/2012

表达式返回06/13/2012

#4


8  

If you like doing things the hard way:

如果你喜欢做一些困难的事情:

my (undef,undef,undef,$mday,$mon,$year) = localtime;
$year = $year+1900;
$mon += 1;
if (length($mon)  == 1) {$mon = "0$mon";}
if (length($mday) == 1) {$mday = "0$mday";}
my $today = "$mon/$mday/$year";

#5


2  

use Time::Piece;
...
my $t = localtime;
print $t->mdy("/");# 02/29/2000

#6


1  

Formating numbers with leading zero is done easily with "sprintf", a built-in function in perl (documentation with: perldoc perlfunc)

用“sprintf”(perl中的一个内置函数)可以很容易地完成与前导零的格式化数字(文档:perldoc perlfunc)

use strict;
use warnings;
use Date::Calc qw();
my ($y, $m, $d) = Date::Calc::Today();
my $ddmmyyyy = sprintf '%02d.%02d.%d', $d, $m, $y;
print $ddmmyyyy . "\n";

This gives you:

这给你:

14.05.2014

14.05.2014

#7


0  

Perl Code for Unix systems:

Unix系统的Perl代码:

# Capture date from shell
my $current_date = `date +"%m/%d/%Y"`;

# Remove newline character
$current_date = substr($current_date,0,-1);

print $current_date, "\n";