我寫了一個Perl腳本來獲取日期時間。 它可以工作,但我不知道是否有更簡單的方法來格式化日期作爲輸出。簡單的方式來格式化日期
#!/usr/bin/perl
use DateTime;
my $dt = DateTime->now(time_zone => 'local');
$myTimeStamp = $dt->subtract(days => 1);
$myYear = $myTimeStamp->year;
$myMonth = $myTimeStamp->month;
if (length($myMonth) == 1) {
$myMonth = "0$myMonth";
}
$myDay = $myTimeStamp->day;
if (length($myDay) == 1) {
$myDay = "0$myDay";
}
$myHour = $myTimeStamp->hour;
if (length($myHour) == 1) {
$myHour = "0$myHour";
}
#$myDir = "/var/tmp/logs/$myYear/$myMonth/$myYear$myMonth-";
print "--> $myYear $myMonth $myDay $myHour\n";
# --> 2012 02 28 02
exit 0;
您在$ myTimeStamp中有一個DateTime對象。 DateTime對象有一個'strftime'方法。使用它:) – 2012-02-29 16:45:21