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