linux date YYMMDD,date打印指定的时间段YYYYMMDD

date打印指定的时间段YYYYMMDD

2014-03-26

有时我们需要生成一段时间内的YYYYMMDD格式的字符串。

方法1:利用unix时间戳

#!/bin/bash

#date.sh

beg_date=`date -d "$1" +%s`

end_date=`date -d "$2" +%s`

if [[ -z $1 ]]||[[ -z $2 ]];then

echo "Usage: $0 YYYYMMDD YYYYMMDD"

exit 0;

fi

if [[ ${beg_date} > ${end_date} ]];then

echo "The end_date < beg_date ;Please input the right date,example 20140101 20140301"

exit 0;

fi

for (( i=${beg_date};i<=${end_date};i=i+86400))

do

date -d @${i} +%Y%m%d

done

##############################################################

或者将for替换为While

while [ "$beg_date" -le "$end_date" ]

do date -d @${beg_date} +%Y%m%d

beg_date=$((beg_date+86400))

done 方法

2:利用date +%Y%m%d -d 20140101" +1 days"

#!/bin/sh

beg_date=$1

end_date=$2

i=1

if [[ -z $1 ]]||[[ -z $2 ]];then

echo "Usage: $0 YYYYMMDD YYYYMMDD"

exit 0;

fi

if [[ ${beg_date} > ${end_date} ]];then

echo "The end_date < beg_date ;Please input the right date,example 20140101 20140301"

exit 0;

fi

while [[ ${beg_date} -le ${end_date} ]];

do

echo $beg_date;

beg_date=`date +%Y%m%d -d ${beg_date}" ${i} days"`

i=i+1

done

####################################################################

./date 20140227 20140305

20140227

20140228

20140301

20140302

20140303

20140304

其他:只打印YYYYMM年月

#!/bin/sh

. ~/.bash_profile

beg_date=37

end_date=45

for (( i=${beg_date};i<=${end_date};i++))

do

j=$(expr $i \* -1)

ym=`date -d ${j}month +%Y%m`

sqlplus 'data/data123456' << EOF

truncate table CHAT_${ym};

drop table CHAT_${ym} purge;

DROP TABLESPACE CHAT_DATA_TD_${ym} INCLUDING CONTENTS AND DATAFILES;

EOF

done

相关日志

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值