shell根据当前时间获取当月月初零点时间戳和月末时间戳


#!/bin/sh

#根据当前时间获取当前月份的第一天零点时间戳和下一月份零点时间戳
# ./getMonth.sh

#根据输入的时间戳
#./getMonth.sh -t 1345600232

#根据输入的日期
#./getMonth.sh -d 2012-12

if [ $1 ]
then
    if [ $1 = '-t' ]
    then
        if [ $2 ]
        then
            month=`date -d@$2 +%Y-%m`
        else
            month=`date +%Y-%m`
        fi
    elif [ $1 = '-d' ]
    then
        if [ $2 ]
        then
            month=$2
        else
            month=`date +%Y-%m`
        fi
    else
            month=`date +%Y-%m`
    fi
else
    month=`date +%Y-%m`
fi


startMonth=${month}-01
nextMonth=`date -d "${startMonth} next month" +%Y-%m`-01

startTime=`date -d "$startMonth" +%s`
endTime=`date -d "$nextMonth" +%s`

echo $month

echo `date -d@$startTime "+%Y-%m-%d %H:%M:%S"`
echo $startTime

echo `date -d@$endTime "+%Y-%m-%d %H:%M:%S"`
echo $endTime

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM