持之以恒

记录本
posts - 4, comments - 32, trackbacks - 0, articles - 74
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

Perl格式化输出时间日期

Posted on 2012-03-28 15:10 小白19870626 阅读(3024) 评论(0)  编辑  收藏 所属分类: linux

方法一:

use POSIX qw(strftime);

 

my $timeStr1 = strftime "%Y-%m-%d", localtime;

my $timeStr2 = strftime "%Y-%m-%d", localtime(time-86400); (前一天)

print "$timeStr \n";

 

 

 

 

 

方法二:

 

#!/usr/bin/perl

($sec,$min,$hour,$day,$mon,$year,$weekday,$yeardate,$savinglightday)

= (localtime(time));

$sec = ($sec < 10)? "0$sec":$sec;

$min = ($min < 10)? "0$min":$min;

$hour = ($hour < 10)? "0$hour":$hour;

$day = ($day < 10)? "0$day":$day;

$mon = ($mon < 9)? "0".($mon+1):($mon+1);

$year += 1900;

$today = "$day.$mon.$year|$hour:$min:$sec";

print $today."\n";

print time."\n";

如果要输出一天前的日期,将time减去一天的秒数(86400)。

(localtime(time-86400));

#!/usr/bin/perl

print &get_time(10)."\n";

sub get_time {

$interval = $_[0]*60;

($sec,$min,$hour,$day,$mon,$year,$weekday,$yeardate,$savinglightday)

= (localtime(time + $interval));

$sec = ($sec < 10)? "0$sec":$sec;

$min = ($min < 10)? "0$min":$min;

$hour = ($hour < 10)? "0$hour":$hour;

$day = ($day < 10)? "0$day":$day;

$mon = ($mon < 9)? "0".($mon+1):($mon+1);

$year += 1900;

return "$year-$mon-$day $hour:$min:$sec.00";

}

sub getTime(){

(my $sec,my $min,my $hour,my $day,my $mon,my $year,my $weekday,my $yeardate,my $savinglightday)

= (localtime(time));

$sec = ($sec < 10)? "0$sec":$sec;

$min = ($min < 10)? "0$min":$min;

$hour = ($hour < 10)? "0$hour":$hour;

$day = ($day < 10)? "0$day":$day;

$mon = ($mon < 9)? "0".($mon+1):($mon+1);

$year += 1900;

my $now = "$year$mon$day $hour:$min:$sec ";

return $now;

}



小白

只有注册用户登录后才能发表评论。


网站导航:
博客园   IT新闻   Chat2DB   C++博客   博问