加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Perl 获取上月下月和最大天数

发布时间:2020-12-16 00:10:41 所属栏目:大数据 来源:网络整理
导读:????????????????? 获取上月,下月,当月的天数,比较常用,故现在整理并分享给大家。 ? ? #! /usr/bin/perl -w use warnings; use strict; use POSIX qw(strftime); use Time::Local; use POSIX qw(ceil floor); my $strtime='2013-12-01'; 调用格式: ----

????????????????? 获取上月,下月,当月的天数,比较常用,故现在整理并分享给大家。

?

?

#! /usr/bin/perl -w
use warnings;
use strict;
use POSIX qw(strftime);
use Time::Local;
use POSIX qw(ceil floor);

my $strtime='2013-12-01';

调用格式:

----------------------------------------------------------------------------------------

my $Submonth=&getSub_month($strtime,1);
print $Submonth."n";
$Submonth=&getSub_month($strtime,12);
print $Submonth."n";

my $Submonth=&getAdd_month($strtime,1);

print $Submonth."n";

$Submonth=&getAdd_month($strtime,12);

print $Submonth."n";

$Submonth=&getlastmonday($strtime);
print $Submonth."n";
$Submonth=&getmaxmonday($strtime);
print $strtime," ....",$Submonth,"n";

----------------------------------------------------------------------------------------------------

?

相关函数和过程:

sub getSub_month
?{
## this step must less then 12,
?my ($strtime,$step)=@_;
?my ($y,$m,$d) = split('-',$strtime);
##timelocal($sec,$min,$hour,$mday,$mon,$year);
?my $unixtime=timelocal(0,$d,$m-1,$y);

?my ($sec,my $min,my $hour,my $day,my $mon,my $year,my $weekday,my $yeardate,my $savinglightday) = (localtime($unixtime));
?$day = '01';
?$mon-=$step;
?if($mon<0)
?? {
??? $year-=1;
??? $mon+=12;
?}
?$mon = ($mon < 9)? "0".($mon+1):($mon+1);
?$year += 1900;
?my $now="$year-$mon-$day";
?return $now;
}

?

sub getAdd_month
?{
## this step must less then 12,my $savinglightday) = (localtime($unixtime));
?$day = '01';
?$mon+=$step;
?if($mon>11)
?? {
??? $year+=1;
??? $mon-=12;
?}
?$mon = ($mon < 9)? "0".($mon+1):($mon+1);
?$year += 1900;
?my $now="$year-$mon-$day";
?return $now;
}

sub getlastmonday
{
?my ($curmon)=@_;
?my $nextmon=&getAdd_month($curmon,1);
?my ($y,$nextmon);
?my $unixtime=timelocal(0,$y);
??? $unixtime-=24*60*60;? ##get before day
?my ($sec,my $savinglightday) = (localtime($unixtime));

?$day = ($day < 10)? "0$day":$day;
?$mon = ($mon < 9)? "0".($mon+1):($mon+1);
?$year += 1900;
?my $now = "$year-$mon-$day";
?return $now;
}

sub getmaxmonday
{
?my ($curmon)=@_;
?my $nextmon=&getAdd_month($curmon,my $savinglightday) = (localtime($unixtime));
?my $now = "$day";
?return $now;
}

?

sub getTime(){
?(my $sec,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;
}

?

以上函数可以方便的计算指定日期的上N个月,下N 个月,和获取指定日期的月份最后一日和指定日期月份有多少天。

现在的函数适合月份跨度是12个月内的。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读