Shell 获取本月最后一天
生活随笔
收集整理的這篇文章主要介紹了
Shell 获取本月最后一天
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Shell 常用日期計算
- 日期格式化
- 獲取當前日期
- 獲取月初、月末系列
- 獲取天數系列
在 shell 中,經常要計算各種各樣的日期,將平時常用的計算日期方法記錄了下來
如有不對,請及時指正
日期格式化
將格式為 yyyyMMdd 的日期格式化為 yyyy-MM-dd
curr_date=20220130 curr_month_begin=`date -d "${curr_date}" "+%Y-%m-%d"`獲取當前日期
獲取今天的日期,格式為 yyyyMMdd
curr_date=`date "+%Y%m%d"` echo $curr_date獲取月初、月末系列
獲取本月第一天
curr_date=20220130 curr_month_begin=`date -d "${curr_date}" "+%Y%m01"`
獲取本月最后一天
思路:先獲取本月第一天,接著求下月第一天,然后下月第一天減去一天
curr_date=20220130 curr_month_begin=`date -d "${curr_date}" "+%Y%m01"` next_month_begin=`date -d "${curr_month_begin} +1 month" "+%Y%m01"` curr_month_end=`date -d "${next_month_begin} -1 day" "+%Y%m%d"`獲取上個月第一天
思路:先獲取本月第一天,然后減去一個月
curr_date=20220130 curr_month_begin=`date -d "${curr_date}" "+%Y%m01"` pre_month_begin=`date -d "${curr_month_begin} -1 month" "+%Y%m%d"`
獲取上個月最后一天
思路:先獲取本月第一天,然后減去一天
curr_date=20220130 curr_month_begin=`date -d "${curr_date}" "+%Y%m01"` pre_month_end=`date -d "${curr_month_begin} -1 day" "+%Y%m%d"`獲取天數系列
獲取本月的天數
思路:求出當月最后一天,然后最后一天的數字即天數
curr_date=20220130 curr_month_begin=`date -d "${curr_date}" "+%Y%m01"` next_month_begin=`date -d "${curr_month_begin} +1 month" "+%Y%m01"` curr_month_end=`date -d "${next_month_begin} -1 day" "+%Y%m%d"` curr_month_days=`date -d "${curr_month_end}" "+%d"`總結
以上是生活随笔為你收集整理的Shell 获取本月最后一天的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2020 智慧消防物联网 之室外智慧消
- 下一篇: python中的逻辑量有什么_pytho