Kapsh
Kapsh

Reputation: 22121

YYYY-MM-DD format date in shell script

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

Upvotes: 1593

Views: 2224467

Answers (18)

Philip Fourie
Philip Fourie

Reputation: 117017

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

Upvotes: 2381

zeg
zeg

Reputation: 586

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

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

Upvotes: 14

Harikrishna
Harikrishna

Reputation: 458

echo "`date "+%F"`"

Will print YYYY-MM-DD

Upvotes: 5

Dwarak Varadarajan
Dwarak Varadarajan

Reputation: 22

Try this code for a simple human readable timestamp:

dt=$(date)
echo $dt

Output:

Tue May 3 08:48:47 IST 2022

Upvotes: -5

Anto
Anto

Reputation: 3916

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

Upvotes: 11

kenm
kenm

Reputation: 23965

Try: $(date +%F)

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

Upvotes: 555

ahmettolga
ahmettolga

Reputation: 1176

$(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

Upvotes: 100

Abdallah_98
Abdallah_98

Reputation: 1373

Try to use this command :

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

The output would be like: 21-Feb-2021

Upvotes: 7

arp
arp

Reputation: 299

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.

Upvotes: 9

Lu&#237;s de Sousa
Lu&#237;s de Sousa

Reputation: 6841

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

Upvotes: 23

Miguel Q
Miguel Q

Reputation: 3628

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

Upvotes: 9

wjandrea
wjandrea

Reputation: 33145

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.

Upvotes: 24

Farid Haq
Farid Haq

Reputation: 4209

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

Upvotes: 0

ankitbaldua
ankitbaldua

Reputation: 273

#!/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.

Upvotes: 6

kgui
kgui

Reputation: 4165

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

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

Upvotes: 13

xu2mao
xu2mao

Reputation: 593

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

The output would be 2015-06-14.

Upvotes: 28

Medievalist
Medievalist

Reputation: 743

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)

Upvotes: 58

lutz
lutz

Reputation:

You can do something like this:

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

Upvotes: 179

Related Questions