1562

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

5
  • 24
    Comments must be at the least 15 words in length. date -I
    – abc
    Commented Oct 15, 2020 at 5:12
  • 35
    Indeed date -I is all you need. It took me years to stumble upon that. Commented Mar 15, 2021 at 23:28
  • 9
    date -I only works on Linux using GNU tools, not on macOS / BSD; installing brew --> brew install coreutils makes gdate -I available
    – ssc
    Commented Jul 29, 2021 at 18:05
  • date -r <TIMESTAMP> -I works fine under the BSD tools FWIW.
    – user10470
    Commented Jan 23 at 20:01
  • 5
    2024 update - date -I now works on macOS Commented Apr 13 at 9:27

18 Answers 18

2352

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
19
  • 6
    In the first days of the month I get "2012-07-1" which is not what the OP asks for.
    – DerMike
    Commented Jul 2, 2012 at 9:29
  • 46
    DATE=$(date +%d-%m-%Y" "%H:%M:%S); What I ended up after.
    – JacopKane
    Commented Mar 14, 2015 at 4:53
  • 11
    I haven't checked how widely available these shortcuts are, but in some distributions you can use +%F %T as a shortcut for +%Y-%m-%d %H:%M:%S. Just note that some filesystems (cough**HFS) will convert the : to a /, giving you a string like 2016-09-15 11/05/00 which is mighty confusing.
    – beporter
    Commented Sep 15, 2016 at 16:07
  • 37
    The preferred syntax in any POSIX-compliant shell in this millennium is date=$(date) instead of date=`date`. Also, don't use uppercase for your private variables; uppercase variable names are reserved for the system.
    – tripleee
    Commented Sep 26, 2016 at 5:53
  • 5
    man date does not explain the + which indicates the beginning of the format string.
    – Timo
    Commented Jan 29, 2018 at 8:41
540

Try: $(date +%F)

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

2
  • 19
    The man pages for date reads: %F full date; same as %Y-%m-%d, so this is just a more compact notation for the accepted answer. Commented Nov 16, 2015 at 20:42
  • 2
    This answer works in zsh as well.
    – Glen K
    Commented May 12, 2023 at 23:10
172

You can do something like this:

$ date +'%Y-%m-%d'
1
  • 2
    By far the easiest to remember and most configurable method... less is more!
    – mirekphd
    Commented Sep 15, 2023 at 18:55
96
$(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
3
  • 5
    Appreciate the addtional time - OP may not have asked, but I'm sure most people googling this are interested! Commented Oct 28, 2021 at 19:30
  • 8
    %T is an useful alias for %H:%M:%S, too.
    – mgutt
    Commented Nov 27, 2021 at 15:12
  • 2
    Thanks also @mgutt! I went for $(date '+%F_%T') in the end.
    – Teun M.
    Commented Jun 14, 2023 at 9:55
58

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
  • 10
    I have a recent (>1988) Mac OS X computer, and date -I didn't work. Having installed GNU coreutils using brew (which uses the prefix 'g') gdate -I did work.
    – Joel Purra
    Commented Aug 23, 2013 at 15:47
  • 4
    Odd. I can't find the -I option documented for GNU date, although sure enough it does seem to be equivalent to date +%F.
    – chepner
    Commented Oct 14, 2013 at 21:55
  • 4
    OS X is generally a GPL v3 wasteland, so they might just not have updated date or BASH recently.
    – Indolering
    Commented Dec 16, 2013 at 20:50
  • 1
    I like this option because it doesn't require escaping % in cron.
    – simlev
    Commented Jun 29, 2020 at 9:46
28
date -d '1 hour ago' '+%Y-%m-%d'

The output would be 2015-06-14.

2
  • 5
    Wrong for a couple of reasons, obviously this gives the wrong date between 00:00 and 01:00, and besides you end with a %. Commented Apr 18, 2016 at 15:50
  • it's not true. should be like this sparse single quote
    – Beyhan Gul
    Commented Jul 27, 2017 at 7:38
24

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.

5
  • 5
    As a note in 2019, this command is incredibly faster* than date if you are using this from within a bash script already, as it doesn't have to load any extra libraries. (* I measured on my linux server a ~160x speed difference over 1000 iterations)
    – timtj
    Commented May 24, 2019 at 13:11
  • @timtj Thanks for pointing that out! I added some notes about speed to the answer.
    – wjandrea
    Commented May 24, 2019 at 14:35
  • 1
    I wish I could +5 for the comment about printf -v date not forking a subshell. Very good info!!
    – timtj
    Commented May 26, 2019 at 12:47
  • Usually I use date because I also need to increment through some dates. Can printf do date arithmetic?
    – Merlin
    Commented Sep 30, 2019 at 22:07
  • 1
    @Merlin I don't think so
    – wjandrea
    Commented Sep 30, 2019 at 22:52
21

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
1
  • 2
    This answer seems equivalent to this other one, which actually uses the other syntax $( … ) for command substitution; see e.g. GreyCat's BashFAQ #082 for details.
    – ErikMD
    Commented Aug 4, 2021 at 20:31
13

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

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

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
9

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

9

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.

0
9

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

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

7

Try to use this command :

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

The output would be like: 21-Feb-2021

2
  • 1
    this is the worst method ever! Not only it's longer and slower, it also doesn't work at all for most locales. Try LC_ALL=C.UTF-8 date, LC_ALL=de_DE.UTF-8 date, LC_ALL=fr_FR.UTF-8 date, LC_ALL=ru_RU.UTF-8 date, or LC_ALL=el_GR.UTF-8 date... for example. It doesn't even work for all English locales
    – phuclv
    Commented Sep 27, 2022 at 7:09
  • 1
    date outputs a human-readable date in the current locale, so parsing it is as useless as parsing apt install output
    – phuclv
    Commented Sep 27, 2022 at 15:51
6
#!/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.

4
echo "`date "+%F"`"

Will print YYYY-MM-DD

1
  • 1) There were already so many answers on %F. 2) This is wrong & redundant in so many levels: using backticks is deprecated, see the unreadable quotes? using $() would be much more readable; there's no need to spawn a subshell, capture its output and print with echo when a single date "+%F" in the current shell is already enough
    – phuclv
    Commented Sep 27, 2022 at 15:48
0

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} -------------"
1
  • backticks`` are obsolete and deprecated long ago. Never use them, use $() instead which is nestable and readable
    – phuclv
    Commented Sep 27, 2022 at 15:42
-5

Try this code for a simple human readable timestamp:

dt=$(date)
echo $dt

Output:

Tue May 3 08:48:47 IST 2022
1
  • 2
    the OP asked specifically for the YYYY-MM-DD format
    – phuclv
    Commented Sep 27, 2022 at 15:49

Not the answer you're looking for? Browse other questions tagged or ask your own question.