在实际开发过程中会用到一些特定时间,请注意其中下月1日和上月同期日期无法用shell命令直接获取,需做判断。
1.获取系统当前时间 today=`date +%Y%m%d` 2.本月1日 firstday=`date -d "${today}" +%Y%m01` 3.本月月份 month=`date -d "${today}" +%m` 4.上月月份 l_month=`date -d "${l_lastday}" +%Y%m` l_month2=`date -d "${l_lastday}" +%Y/%m` 5.下月1日 不能直接用n_month=`date -d "${today} +1 month" +%Y%m01` 当today=20190131时,用此公式n_month=20190301 需做判断 n_month1=`date -d "$today +1 month" +%m` n_month2=$[ $month + 1 ] if [ $n_month2 -ge $n_month2 ] then n_month=`date -d "${today} +1 month" +%Y%m01` else if [ $n_month2 -lt $n_month2 ] then n_month=`date -d "${today} +20 day" +%Y%m01` fi 6.本月月底 MonthEnd=`date -d "$n_month -1 day" +%Y%m%d` 7.上月月底 l_lastday=`date -d "${firstday} last day" +%Y%m%d` 8.上月同期日期last_month_date 不可以直接用l_month_date=`date -d "$today -1 month" +%Y%m%d` 特殊情况时,不能取到正确的值,如today=20190331时,l_month_date=20190303,因为3月有31天,2月只用28天,当today=20190331时,对应的上个月同期日期应该取2月份的最后一天。(在实际应用中计算绩效时上月同期指标值会用到) 需用上月月底l_lastday和l_month_date做比较判断: if [ $l_lastday -ge $l_month_date ] then last_month_date=$l_month_date else last_month_date=$l_lastday fi
12345678910111213141516171819202122232425262728293031323334353637