perl使用sprintf格式化获得的本地时间

发布时间:2020-09-16编辑:脚本学堂
perl使用sprintf格式化获得的本地时间

脚本
 

复制代码 代码如下:
#!/usr/bin/perl -w
my $local_time=&GetTimestamp;
print "Local Time: $local_timen";
sub GetTimestamp
{
  my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
  $year=$year+1900;
  return $year .  sprintf('%02u%02u_%02u%02u%02u', $mon+1, $mday, $hour, $min, $sec);
}

perl sprintdate.pl
Local Time: 20111222_150720

脚本二
 

复制代码 代码如下:
#!/usr/bin/perl -w
use strict;

#my $date=localtime;
#print "$daten";

sub GetDateToday
{
  my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
  $year=$year+1900;
  return $year.'-'.sprintf('%02u-%02u', $mon+1, $mday);
}

my $cur_date=GetDateToday();
print "$cur_daten";
print "$^On";

perl date.pl

2011-12-22
linux

注:$^O 表示当前系统