I tried using $(date)
in my bash shell script, however, I want the date in YYYY-MM-DD
format.
How do I get this?
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).
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
-
4In the first days of the month I get "2012-07-1" which is not what the OP asks for. – DerMike Jul 2 '12 at 9:29
-
6This works for me. Man page suggests that the first of the month is 01, no 1 (Ex. 2012-07-01). – Bob Kuhar Mar 19 '13 at 18:59
-
35
-
5I 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 like2016-09-15 11/05/00
which is mighty confusing. – beporter Sep 15 '16 at 16:07 -
27The preferred syntax in any POSIX-compliant shell in this millennium is
date=$(date)
instead ofdate=`date`
. Also, don't use uppercase for your private variables; uppercase variable names are reserved for the system. – tripleee Sep 26 '16 at 5:53
Try: $(date +%F)
-
13The 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. – Håvard Geithus Nov 16 '15 at 20:42
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)
-
9I 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 Aug 23 '13 at 15:47 -
3Odd. I can't find the
-I
option documented for GNUdate
, although sure enough it does seem to be equivalent todate +%F
. – chepner Oct 14 '13 at 21:55 -
3OS X is generally a GPL v3 wasteland, so they might just not have updated date or BASH recently. – Indolering Dec 16 '13 at 20:50
-
$(date +%F_%H-%M-%S)
can be used to remove colons (:) in between
output
2018-06-20_09-55-58
date -d '1 hour ago' '+%Y-%m-%d'
The output would be 2015-06-14
.
-
5Wrong for a couple of reasons, obviously this gives the wrong date between 00:00 and 01:00, and besides you end with a
%
. – Gerhard Burger Apr 18 '16 at 15:50 -
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.
-
4As 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 May 24 '19 at 13:11 -
@timtj Thanks for pointing that out! I added some notes about speed to the answer. – wjandrea May 24 '19 at 14:35
-
1I wish I could +5 for the comment about
printf -v date
not forking a subshell. Very good info!! – timtj May 26 '19 at 12:47 -
Usually I use date because I also need to increment through some dates. Can printf do date arithmetic? – Merlin Sep 30 '19 at 22:07
-
1
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
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.
if you want the year in a two number format such as 17 rather than 2017, do the following:
DATE=`date +%d-%m-%y`
Try to use this command :
date | cut -d " " -f2-4 | tr " " "-"
The output would be like: 21-Feb-2021
#!/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.
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} -------------"