bash shell 脚本中的 YYYY-MM-DD 格式日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1401482/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
YYYY-MM-DD format date in shell script
提问by Kapsh
I tried using $(date)
in my bash shell script, however, I want the date in YYYY-MM-DD
format.
How do I get this?
我尝试$(date)
在我的 bash shell 脚本中使用,但是,我想要YYYY-MM-DD
格式的日期。
我怎么得到这个?
回答by Philip Fourie
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).
在 bash (>=4.2) 中,最好使用 printf 的内置日期格式化程序(bash 的一部分)而不是外部date
(通常是 GNU 日期)。
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):
在 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):
可以从日期手册页查看其他可用的日期格式(对于外部非 bash 特定命令):
man date
回答by kwatford
Try: $(date +%F)
尝试: $(date +%F)
回答by kwatford
You can do something like this:
你可以这样做:
$ date +'%Y-%m-%d'
回答by Medievalist
回答by xu2mao
date -d '1 hour ago' '+%Y-%m-%d'
The output would be 2015-06-14
.
输出将是2015-06-14
.
回答by ahmettolga
$(date +%F_%H-%M-%S)
can be used to remove colons (:) in between
可用于删除中间的冒号 (:)
output
输出
2018-06-20_09-55-58
回答by wjandrea
With recent Bash (version ≥ 4.2), you can use the builtin printf
with the format modifier %(strftime_format)T
:
使用最近的 Bash(版本 ≥ 4.2),您可以使用printf
带有格式修饰符的内置函数%(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.
printf
比date
因为它是 Bash 内置date
而是外部命令快得多。
As well, printf -v date ...
is faster than date=$(printf ...)
since it doesn't require forking a subshell.
同样,printf -v date ...
比date=$(printf ...)
因为不需要分叉子shell而更快。
回答by arp
Whenever I have a task like this I end up falling back to
每当我有这样的任务时,我最终都会回到
$ man strftime
to remind myself of all the possibilities.
提醒自己所有的可能性。
回答by Crt
if you want the year in a two number format such as 17 rather than 2017, do the following:
如果您希望年份采用两个数字格式,例如 17 而不是 2017,请执行以下操作:
DATE=`date +%d-%m-%y`
回答by ankitbaldua
#!/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.
这里 x 是使用的示例日期,然后示例显示数据格式以及获取比当前日期多 10 分钟的日期。