如何在Bash中使用date命令处理日期和时间

日期命令是一個外部的 bash 程序,允許設置或顯示系統日期和時間。它還提供了幾個格式化選項。日期命令在所有 Linux 發行版中都默認安裝。

$ which date
$ type -a date
Find Date Command Location

在終端中輸入 date 命令,它將顯示當前日期和時間。

$ date
Check Date in Linux

更改 Linux 系統日期和時間

使用 date 命令,可以修改系統日期、時間和時區,並將更改同步到硬件時鐘。

$ date --set="Thu Nov 12 13:06:59 IST 2020"
$ hwclock --systohc
Set Linux System Date and Time

格式化選項

A good place to get the list of formatting options will be the man page.

$ man date

讓我們看一些最常用的格式化選項。

  • 要應用格式化,請使用 “+“,後跟 “格式“。
  • 要獲取 GNU\LINUX 的格式化選項列表,請參考連結的 man 頁面
  • 要獲取 BSD 的格式化選項列表,請參考連結的 man 頁面

日期命令的兩個重要部分是使用格式 +% 和 –date 選項。

現在讓我們對 date 命令 應用一些格式。要應用格式,請添加加號 (+),然後跟隨 %格式,如示例所示。

在 Linux 中處理日期

讓我們來看看如何在一個名為「date.sh」的簡單 shell 腳本中使用日期相關格式。

# PRINT YEAR,MONTH,DAY AND DATE...

echo "We are in the year = $(date +%Y)"
echo "We are in the year = $(date +%y)"

# Difference between %Y and %y is %Y will print 4 digits while %y will print the last 2 digits of the year.

echo "We are in the month = $(date +%m)"
echo "We are in the month = $(date +%b)"
echo "We are in the month = $(date +%B)"

# Difference between %B and %b is, %B will print full month name while %b will print abbreviated month name.

echo "Current Day of the month = $(date +%d)"

echo "Current Day of the week = $(date +%A)"
echo "Current Day of the week = $(date +%a)"

# Difference between %A and %a is, %A will print full Weekday name while %a will print abbreviated weekday name.

# Instead of formatting to get the date, we can use %D which will print the date as %m/%d/%y or %F which prints in %Y-%M-%d format.

echo "Date using %D = $(date +%D)"
echo "Date using %F = $(date +%F)"
Find Date and Time using Script

在 Linux 中處理時間

讓我們來看看如何在一個名為「time.sh」的簡單 shell 腳本中使用時間相關格式。

# PRINT HOURS, MINS, SECONDS, NANO SECONDS

echo Hours = $(date +%H)
echo Minutes = $(date +%M)
echo Seconds = $(date +%S)
echo Nanoseconds = $(date +%N)
echo Epoch Time = $(date +%s)

echo "current time = $(date +%H:%M:%S:%N)"

# can also use %T which displays Time in HH:MM:SS format.

echo "current time in 24 hour format = $(date +%T)"

# can also use %r to display time in 12 hour format.

echo "current time in 12 hour format = $(date +%r)"
Find Time in Linux Using Script

使用 –date 或 -d 標誌

使用 --date-d 標誌輸入可以作為字符串傳遞,date 命令 知道如何聰明地處理它。

讓我們看看一些例子,以了解它是如何工作的。

# Print yesterday's date and time.
echo "Yesterday = $(date -d "Yesterday")"

# Print Tomorrow date and time.
echo "tomorrow = $(date -d "tomorrow")"

# Find what is the date and time before 10 days from now.
echo "Before 10 days = $(date -d "tomorrow -10 days")"

# Find last month and next month
echo "Last month = $(date -d "last month" "%B")"
echo "Next month = $(date -d "next month" "%B")"

# Find last year and next year
echo "Last Year = $(date -d "last year" "+%Y")"
echo "Next Year = $(date -d "next year" "+%Y")"

# Forecast the weekday
echo "2 days away from today and it comes on weekdays? = $(date -d "Today +2 days" "+%A")
Check Date Using Formatting

常見操作

計算給定日期之間的天數。

$ echo $(( ( $(date -d "2020-11-10" "+%s") - $(date -d "2020-11-01" "+%s") ) / 86400))

查找給定年份是否為閏年。

$ for y in {2000..2020}; do date -d $y-02-29 &>/dev/null && echo $y is leap year; done
Find Leap Year in Linux

將 date 命令的輸出分配給一個變量。

$ TODAY=$(date +%Y-%m-%d)
OR
$ TODAY1=$(date +%F)
$ echo $TODAY 
$ echo $TODAY1
Assign Date to Variable

在文件名中添加日期以創建日誌文件。

在創建日誌文件、備份或文本文件時添加日期和時間是一個常見的操作,我們最常會遇到。讓我們舉個例子,為了進行備份,我們創建了一個 shell 腳本。

此腳本將在每天的 00:00 至 23:59 進行備份,並安排在第二天的 00:00 定期運行。我們想要創建帶有昨天日期格式的日誌文件。

CUSTOM_FORMAT=$(date --date "Yesterday" "+%d-%y-%H:%M")
LOG_FILE=/var/log/custom_application/application_${CUSTOM_FORMAT}.log
echo "Script started" >>  ${LOG_FILE}
...
CODE BLOCKS
...
echo "Script completed" >> ${LOG_FILE}

這篇文章就到這裡了。在這篇文章中,我們已經看到了如何在Linux中使用bash日期和時間。讓我們知道您的反饋。

Source:
https://www.tecmint.com/change-linux-system-date-and-time/