01. Get the current time, year, month, day, hour, minute, second
now=$(date +%Y%m%d%H%M%S)
echo "$now"
The output is: 20181202222727
02. Several uses of date in scripts
date +%Y print year in 4 digit format
date +%y print year in 2 digit format
date +%m month
date +%d date date +
%H hour date
+%M minute
date +%S second
date +% w weekday, 0 for Sunday
03. Other usages, get the time before the current time
If you want to learn automated testing, here I recommend a set of videos for you. This video can be said to be the number one automated testing tutorial on the entire network at station B. At the same time, the number of people online has reached 1,000, and there are notes to collect and share with you. Dashen Technical Exchange: 798478386
The first way of writing:
Get the number in front of day, hour, minute, second day: negative number is the previous few days, positive number is the next few days, minutes and seconds are the same
date -d "-1 hour" +%H 1 hour before
date -d "- 1 min" +%M 1 minute before
date -d "-1 second" +%S 1 second before
date -d "-n day" +%d Get the date of the previous n days
date -d "n days" +%Y%m%d Get the time after n days
date -d "3 years ago" +%Y%m%d Get the time 3 years ago
date -d "3 years" +%Y%m%d Get the time 3 years later
date -d "3 month ago" +%m get 3 months ago
date -d "3 month" +%Y%m get 3 months later
The second way of writing:
date -d "n days ago" +%d Get the date n days ago
date -d "-n days ago" +%Y%m%d Get the date n days later
date -d "n month ago" +%d Get the date n months ago
date -d "-n month ago" +%Y%m%d Get the date n months later
date -d "n month ago" +%d Get the date n years ago
date -d "-n month ago" +%Y%m%d Get the date n years later
The third way of writing:
date +%Y%m%d --date="-n day" get the date n days ago
date +%Y%m%d --date="-n day" get the date n days later
date +%Y%m%d --date="n month ago" Get the date n months ago
date +%Y%m%d --date="-n month ago" Get the date n months later
date +%Y%m%d --date="n years ago" Get the date n years ago
date +%Y%m%d --date="-n years ago" Get the date n years later