Format: Date [options] ... [+ format]
Option Description:
-D,--date= string displays the time described by the specified string
Format Description:
Example 1:
#!/bin/bash##1. Gets the current system time yyyy-mm-DD HH:MM:SSv_time_eq=`Date "+%f%T"' V_time=`Date "+%y-%m-%d%h:%m:%s"` Echo-E"System Current Time v_time: ${v_time}, V_time_eq: ${v_time_eq} \tv_time is equivalent to v_time_eq, but v_time format, more readable, recommended to use. "##2. Gets the date of the specified format yyyy-mm-dd, YYYYMMDD, yyyy/mm/ddv_date=`Date-D"$v _time"+%y%m%d ' V_date_dir=`Date-D"$v _time"+%y/%m/%d ' V_date_hor=`Date-D"$v _time"+%y-%m-%d 'Echo "System Current date v_date: ${v_date}, V_date_dir: ${v_date_dir}, V_date_hor: ${v_date_hor}. Feel the different formats. "##3. Gets the date of the day before, Yyyymmddv_date_ago_1=`Date-D"$v _date-1 Day"+%y%m%d ' V_date_future_1=`Date-D"$v _date 1 Day"+%y%m%d 'Echo "the day before V_date_ago_1: ${v_date_ago_1}, V_date_future_1: ${v_date_future_1}"##4get the date of the first one months and the last one months Yyyymmddv_date_ago_1month=`Date-D"$v _date-1 Month"+%y%m%d ' #遇到2月闰月的时候, there will be problems, such as 20170329, the first one months will become 20170301v_date_ago_1month_leap=`Date-D"20170329-1 Month"+%y%m%d ' V_date_future_1month=`Date-D"$v _date 1 month"+%y%m%d 'Echo "previous one month date V_date_ago_1month: ${v_date_ago_1month}, last one month date v_date_future_1month: ${v_date_future_1month}" Echo "Leap February, there will be problems, such as 20170329 of the first one months returned is: ${v_date_ago_1month_leap}, not 20170228"##5. Gets the date of the previous year, the following year Yyyymmddv_date_ago_1year=`Date-D"$v _date-1 Year"+%y%m%d ' V_date_future_1year=`Date-D"$v _date 1 year"+%y%m%d 'Echo "date of previous year V_date_ago_1year:${v_date_ago_1year}, date of the following year V_date_future_1year: ${v_date_future_1year}"##6. Get the system current month month date Yyyymmddv_month=`Date-D"$v _date"+%y%m ' onV_month_ago_1=`Date-D"$v _month-1 Month"+%y%m ' onV_month_future_1=`Date-D"$v _month 1 month"+%y%m ' on Echo "system Current Month early Month date V_month:${v_month}, early last month date V_month_ago_1:${v_month_ago_1}, early next month date V_month_future_1:${v_month_ Future_1}"
View Code
Results:
Shell Date Command Detailed