网站搜索

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


Date 命令是一个外部 bash 程序,允许设置或显示系统日期和时间。它还提供了多种格式选项。默认情况下,所有 Linux 发行版中都安装了 Date 命令。

which date
type -a date

在终端中输入日期命令,它将显示当前日期和时间。

date

更改 Linux 系统日期和时间

使用date命令,可以修改系统日期、时间和时区,并且更改必须与硬件时钟同步。

date --set="Thu Nov 12 13:06:59 IST 2020"
hwclock --systohc

格式选项

获取格式选项列表的好地方是手册页。

man date

让我们看看我们将使用的一些最常见的格式选项。

  • 要应用格式设置,请使用“+”,然后使用“格式化程序”。
  • 要获取 GNU\LINUX 的格式化选项列表,请查看链接的手册页。
  • 要获取 BSD 格式化选项列表,请查看链接的手册页。

日期命令的两个重要部分是使用格式+%和-date选项。

现在让我们对日期命令应用一些格式。要应用格式设置,请添加加号 (+),后跟 %formatter,如示例所示。

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)"

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)"

使用 –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")

常用操作

计算两个给定日期之间的天数。

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

将日期命令的输出分配给变量。

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

创建日志文件,并将日期添加到文件名中。

在创建日志文件、备份或文本文件时添加日期和时间是我们最常遇到的常见操作。举个例子,为了进行备份,我们创建了一个 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 日期和时间。让我们知道您的反馈。