YYYY-MM-DD format date in shell script

2019-01-03 18:50发布

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

11条回答
forever°为你锁心
2楼-- · 2019-01-03 19:48

You can do something like this:

$ date +'%Y-%m-%d'
查看更多
Bombasti
3楼-- · 2019-01-03 19:48

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)

查看更多
劳资没心,怎么记你
4楼-- · 2019-01-03 19:50
$(date +%F_%H-%M-%S)

can be used to remove colons (:) in between

output

2018-06-20_09-55-58
查看更多
Lonely孤独者°
5楼-- · 2019-01-03 19:51
#!/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.

查看更多
对你真心纯属浪费
6楼-- · 2019-01-03 19:52

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} -------------"
查看更多
登录 后发表回答