Organize the date operation script in the shell script
1. Calculate the date of the day before the specified date
date -d "yesterday 20150401 " +%Y%m%d
2. If you get the day before the current date
date -d "yesterday" +%Y%m%d
3. Calculate the first few days of the specified date (for example, calculate the first 10 days of 20190716), if it is a negative number, it will count forward
date -d "10 day ago 20190716" +%Y%m%d
or
date -d "10 day ago 2019-07-16" +%Y%m%d
4. Calculate the first few days of the current date (for example, calculate the first 10 days of the current date), if it is a negative number, it will count forward
date -d "10 day ago" +%Y%m%d
Five, get the time after the specified hour
date -d "20190716 12 3 hour" +"%Y%m%d%H"
Represents the time to move backward three hours at 12 o'clock on July 16, 2019. The running result is: 2019071615
Six, get the time point a few hours after the current time point
date -d "3 hour" +"%Y%m%d%H"
Means to get the time 3 hours after the current time point, and the result is accurate to the hour
Seven, get a few minutes after the specified time point
date -d "20190716 21:15 10 minute" +"%Y%m%d%H%M"
Indicates that the time point of 10 minutes at 21:15 on July 16, 2019, accurate to the minute
8. Get a few minutes after the current time point
date -d "10 minute" +"%Y%m%d%H%M"
Nine, get this month
date +"%Y%m"
10. Get next month
date -d "1month" +"%Y%m"
11. Get last month
date -d "-1month" +"%Y%m"
12. Get yesterday
date -d yesterday # 这样是未经过任何的格式化,比较难看
date -d yesterday +"%Y%m%d" # 这样是经过格式化的
13. Get tomorrow
date -d tomorrw
date -d tomorrw +"%Y%m%d"
date +%Y%m%d -d "+1 day"
date +"%Y%m%d" -d '+1 day' # 这里引号不是必须的
date +%Y%m%d --date "+1 day"
date +"%Y%m%d" --date '+1 day'
14. Designated month
date -d 1May
15. Now
date -d now
或
date
16. Get the date of the current time
1、date +"%Y-%m-%d"
2、date +"%F"
17. Obtain the indicators of the current time
1、date +%H #小时
2、date +%M #分钟
3、date +%S #秒
4、date +%T #时间
5、date +%w #星期
6、date -d "-1 day" +%F # 前一天
18. Print time in shell script
current_time=$(date "+%Y-%m-%d %H:%M:%S")
echo "当前日期是:" ${current_time}