shell 脚本中的 YYYY-MM-DD 格式日期

发布于 2024-08-04 18:08:32 字数 101 浏览 8 评论 0原文

我尝试在 bash shell 脚本中使用 $(date),但是,我希望日期采用 YYYY-MM-DD 格式。
我如何得到这个?

I tried using $(date) in my bash shell script, however, I want the date in YYYY-MM-DD format.
How do I get this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(18

葮薆情 2024-08-11 18:08:33
echo "`date "+%F"`"

将打印 YYYY-MM-DD

echo "`date "+%F"`"

Will print YYYY-MM-DD

假面具 2024-08-11 18:08:33

您可以将日期设置为环境变量,稍后您可以使用它

setenv DATE `date "+%Y-%m-%d"`
echo "----------- ${DATE} -------------"

DATE =`date "+%Y-%m-%d"`
echo "----------- ${DATE} -------------"

You can set date as environment variable and later u can use it

setenv DATE `date "+%Y-%m-%d"`
echo "----------- ${DATE} -------------"

or

DATE =`date "+%Y-%m-%d"`
echo "----------- ${DATE} -------------"
从﹋此江山别 2024-08-11 18:08:33

尝试使用此代码获得简单的人类可读时间戳:

dt=$(date)
echo $dt

输出:

Tue May 3 08:48:47 IST 2022

Try this code for a simple human readable timestamp:

dt=$(date)
echo $dt

Output:

Tue May 3 08:48:47 IST 2022
も让我眼熟你 2024-08-11 18:08:32

在 bash (>=4.2) 中,最好使用 printf 的内置日期格式化程序(bash 的一部分)而不是外部 date (通常是 GNU 日期)。请注意,由于 Windows 上的 fork() 调用缓慢,因此在 Cygwin 中调用子 shell 会出现性能问题。

因此:

# put current date as yyyy-mm-dd in $date
# -1 -> explicit current date, bash >=4.3 defaults to current time if not provided
# -2 -> start time for shell
printf -v date '%(%Y-%m-%d)T\n' -1

# put current date as yyyy-mm-dd HH:MM:SS in $date
printf -v date '%(%Y-%m-%d %H:%M:%S)T\n' -1

# to print directly remove -v flag, as such:
printf '%(%Y-%m-%d)T\n' -1
# -> current date printed to terminal

在 bash (<4.2) 中:

# put current date as yyyy-mm-dd in $date
date=$(date '+%Y-%m-%d')

# put current date as yyyy-mm-dd HH:MM:SS in $date
date=$(date '+%Y-%m-%d %H:%M:%S')

# print current date directly
echo $(date '+%Y-%m-%d')

其他可用的日期格式可以从 日期手册页(用于外部非 bash 特定命令):

man date

In bash (>=4.2) it is preferable to use printf's built-in date formatter (part of bash) rather than the external date (usually GNU date). Note that invoking a subshell has performance problems in Cygwin due to a slow fork() call on Windows.

As such:

# put current date as yyyy-mm-dd in $date
# -1 -> explicit current date, bash >=4.3 defaults to current time if not provided
# -2 -> start time for shell
printf -v date '%(%Y-%m-%d)T\n' -1

# put current date as yyyy-mm-dd HH:MM:SS in $date
printf -v date '%(%Y-%m-%d %H:%M:%S)T\n' -1

# to print directly remove -v flag, as such:
printf '%(%Y-%m-%d)T\n' -1
# -> current date printed to terminal

In bash (<4.2):

# put current date as yyyy-mm-dd in $date
date=$(date '+%Y-%m-%d')

# put current date as yyyy-mm-dd HH:MM:SS in $date
date=$(date '+%Y-%m-%d %H:%M:%S')

# print current date directly
echo $(date '+%Y-%m-%d')

Other available date formats can be viewed from the date man pages (for external non-bash specific command):

man date
不奢求什么 2024-08-11 18:08:32

尝试:$(date +%F)

%F 选项是 %Y-%m-%d 的别名

Try: $(date +%F)

The %F option is an alias for %Y-%m-%d

蓝眼泪 2024-08-11 18:08:32

你可以这样做:

$ date +'%Y-%m-%d'

You can do something like this:

$ date +'%Y-%m-%d'
↘人皮目录ツ 2024-08-11 18:08:32
$(date +%F)

可以

2018-06-20

或者如果您还想要 time:

$(date +%F_%H-%M-%S)

用于删除

输出之间的冒号 (:)

2018-06-20_09-55-58
$(date +%F)

output

2018-06-20

Or if you also want time:

$(date +%F_%H-%M-%S)

can be used to remove colons (:) in between

output

2018-06-20_09-55-58
痴意少年 2024-08-11 18:08:32

您正在寻找 ISO 8601 标准日期格式,因此如果您有 GNU 日期(或任何比 1988 年更现代的日期命令) )只需执行:$(date -I)

You're looking for ISO 8601 standard date format, so if you have GNU date (or any date command more modern than 1988) just do: $(date -I)

倥絔 2024-08-11 18:08:32
date -d '1 hour ago' '+%Y-%m-%d'

输出将为 2015-06-14

date -d '1 hour ago' '+%Y-%m-%d'

The output would be 2015-06-14.

伏妖词 2024-08-11 18:08:32

在最近的 Bash(版本 ≥ 4.2)中,您可以使用带有格式修饰符 %(strftime_format)T 的内置 printf

$ printf '%(%Y-%m-%d)T\n' -1  # Get YYYY-MM-DD (-1 stands for "current time")
2017-11-10
$ printf '%(%F)T\n' -1  # Synonym of the above
2017-11-10
$ printf -v date '%(%F)T' -1  # Capture as var $date

printf 速度更快比 date 因为它是 Bash 内置命令,而 date 是外部命令。

此外,printf -v date ...date=$(printf ...) 更快,因为它不需要分叉子 shell。

With recent Bash (version ≥ 4.2), you can use the builtin printf with the format modifier %(strftime_format)T:

$ printf '%(%Y-%m-%d)T\n' -1  # Get YYYY-MM-DD (-1 stands for "current time")
2017-11-10
$ printf '%(%F)T\n' -1  # Synonym of the above
2017-11-10
$ printf -v date '%(%F)T' -1  # Capture as var $date

printf is much faster than date since it's a Bash builtin while date is an external command.

As well, printf -v date ... is faster than date=$(printf ...) since it doesn't require forking a subshell.

北音执念 2024-08-11 18:08:32

我使用以下公式:

TODAY=`date -I`
echo $TODAY

查看 date 的手册页,还有许多其他有用的选项:

man date

I use the following formulation:

TODAY=`date -I`
echo $TODAY

Checkout the man page for date, there is a number of other useful options:

man date
橪书 2024-08-11 18:08:32

如果您希望年份采用两位数字格式,例如 17 而不是 2017,请执行以下操作:

DATE=`date +%d-%m-%y`

if you want the year in a two number format such as 17 rather than 2017, do the following:

DATE=`date +%d-%m-%y`
雨轻弹 2024-08-11 18:08:32

我用了下面的方法。感谢所有方法/答案

ubuntu@apj:/tmp$ datevar=$(date +'%Y-%m-%d : %H-%M')
ubuntu@apj:/tmp$ echo $datevar
2022-03-31 : 10-48

I used below method. Thanks for all methods/answers

ubuntu@apj:/tmp$ datevar=$(date +'%Y-%m-%d : %H-%M')
ubuntu@apj:/tmp$ echo $datevar
2022-03-31 : 10-48
誰ツ都不明白 2024-08-11 18:08:32

我使用 $(date +"%Y-%m-%d")$(date +"%Y-%m-%d %T") 与时间和小时数。

I use $(date +"%Y-%m-%d") or $(date +"%Y-%m-%d %T") with time and hours.

千纸鹤带着心事 2024-08-11 18:08:32

每当我遇到这样的任务时,我最终都会回

$ man strftime

想起时间格式选项的所有可能性。

Whenever I have a task like this I end up falling back to

$ man strftime

to remind myself of all the possibilities for time formatting options.

养猫人 2024-08-11 18:08:32

date +%Y-%m-%dT%H:%M:%S

将打印类似 2023-07-18T11:09:16 的内容,通常称为 RFC-3339

date +%Y-%m-%dT%H:%M:%S

will print something like 2023-07-18T11:09:16 which is generally known as RFC-3339

完美的未来在梦里 2024-08-11 18:08:32

尝试使用此命令:

date | cut -d " " -f2-4 | tr " " "-" 

输出将类似于:21-Feb-2021

Try to use this command :

date | cut -d " " -f2-4 | tr " " "-" 

The output would be like: 21-Feb-2021

汐鸠 2024-08-11 18:08:32
#!/bin/bash -e

x='2018-01-18 10:00:00'
a=$(date -d "$x")
b=$(date -d "$a 10 min" "+%Y-%m-%d %H:%M:%S")
c=$(date -d "$b 10 min" "+%Y-%m-%d %H:%M:%S")
#date -d "$a 30 min" "+%Y-%m-%d %H:%M:%S"

echo Entered Date is $x
echo Second Date is $b
echo Third Date is $c

这里 x 是使用的样本日期 &然后示例显示数据格式以及获取比当前日期多 10 分钟的日期。

#!/bin/bash -e

x='2018-01-18 10:00:00'
a=$(date -d "$x")
b=$(date -d "$a 10 min" "+%Y-%m-%d %H:%M:%S")
c=$(date -d "$b 10 min" "+%Y-%m-%d %H:%M:%S")
#date -d "$a 30 min" "+%Y-%m-%d %H:%M:%S"

echo Entered Date is $x
echo Second Date is $b
echo Third Date is $c

Here x is sample date used & then example displays both formatting of data as well as getting dates 10 mins more then current date.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文