Chinaunix首页 | 论坛 | 博客
  • 博客访问: 482265
  • 博文数量: 401
  • 博客积分: 244
  • 博客等级: 入伍新兵
  • 技术积分: 2215
  • 用 户 组: 普通用户
  • 注册时间: 2012-08-04 10:02
文章分类

全部博文(401)

文章存档

2013年(37)

2012年(364)

分类:

2012-10-23 14:51:02

原文地址:shell中前一天的日期算法 作者:yfjelley

shell 中计算前一天日期算法

取当月最后一天:
#取当月最后一天日期
month=`date +%m`
year=`date +%Y`
Cal=`cal $month $year`
LastDay=`echo $Cal | awk '{print $NF}'`

#取当前日期
day=`date +%d`

#如果今天是本月最后一天则执行
if [ $LastDay -eq $day ]
then do something
fi




shell计算明天和昨天日期的函数
2004-04-23 15:18 pm
来自:Linux文档
现载:
地址:无名

#返回月份的天数
get_mon_days()
{
Y=`expr substr $1 1 4`
M=`expr substr $1 5 2`

r1=`expr $Y \% 4`
r2=`expr $Y \% 100`
r3=`expr $Y \% 400`

case $M in
01|03|05|07|08|10|12) days=31;;
04|06|09|11) days=30;;
esac
if [ $M -eq 02 ]
then
if [ r1 -eq 0 -a r2 -ne 0 -o r3 -eq 0 ]
then
days=29
else
days=28
fi
fi
echo $days
}
#返回昨天日期
get_before_date()
{
Y=`expr substr $1 1 4`
M=`expr substr $1 5 2`
D=`expr substr $1 7 2`
YY=`expr $Y - 1`
MM=`expr $M - 1`
DD=`expr $D - 1`
MM=`printf "%02d" $MM`
DD=`printf "%02d" $DD`
dd=$Y$MM
dad=`get_mon_days $dd`
be_date=$Y$M$DD
if [ $D -eq 01 ]
then
if [ $M -ne 01 ]
then
be_date=$Y$MM$dad
fi
if [ $M -eq 01 ]
then
be_date=$YY"1231"
fi
fi
echo $be_date

}
#返回明天日期
get_next_date()
{
Y=`expr substr $1 1 4`
M=`expr substr $1 5 2`
D=`expr substr $1 7 2`
YY=`expr $Y + 1`
MM=`expr $M + 1`
DD=`expr $D + 1`
MM=`printf "%02d" $MM`
DD=`printf "%02d" $DD`
r1=`expr $Y \% 4`
r2=`expr $Y \% 100`
r3=`expr $Y \% 400`

next_date=$Y$M$DD

if [ $D -eq 30 ]
then
case $M in
04|06|09|11) next_date=$Y$MM"01";;
esac
fi
if [ $D -eq 31 ]
then
next_date=$Y$MM"01"
case $M in
12) next_date=$YY"0101";;
esac
fi
if [ $M -eq 02 ]
then
if [ r1 -eq 0 -a r2 -ne 0 -o r3 -eq 0 ]
then
if [ $D -eq 29 ]
then
next_date=$Y$MM"01"
fi
else
if [ $D -eq 28 ]
then
next_date=$Y$MM"01"
fi
fi
fi
echo $next_date
}

#返回上一周期 yyyymm
get_before_cycle()
{
Y=`expr substr $1 1 4`
M=`expr substr $1 5 2`
YY=`expr $Y - 1`
MM=`expr $M - 1`

# MM=`printf "%02d" $MM`

be_date=$Y$M

if [ $M -ne 01 ]
then
be_date=$Y$MM
fi
if [ $M -eq 01 ]
then
be_date=$YY"12"
fi
echo $be_date
}

阅读(597) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~