I tried using $(date) in my bash shell script, however, I want the date in YYYY-MM-DD format.
How do I get this?
18 Answers
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 22 Comments
+%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.date=$(date) instead of date=`date`. Also, don't use uppercase for your private variables; uppercase variable names are reserved for the system.man date does not explain the + which indicates the beginning of the format string.Try: $(date +%F)
The %F option is an alias for %Y-%m-%d
2 Comments
zsh as well.You can do something like this:
$ date +'%Y-%m-%d' 1 Comment
$(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 Comments
%T is an useful alias for %H:%M:%S, too.$(date '+%F_%T') in the end.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)
5 Comments
date -I didn't work. Having installed GNU coreutils using brew (which uses the prefix 'g') gdate -I did work.-I option documented for GNU date, although sure enough it does seem to be equivalent to date +%F.% in cron.date -I works well on FreeBSD (v14) and macOS X (15)'date -d '1 hour ago' '+%Y-%m-%d' The output would be 2015-06-14.
2 Comments
%.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 Comments
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)printf -v date not forking a subshell. Very good info!!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 Comment
$( … ) for command substitution; see e.g. GreyCat's BashFAQ #082 for details.#!/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.
Comments
Try to use this command :
date | cut -d " " -f2-4 | tr " " "-" The output would be like: 21-Feb-2021
3 Comments
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 localesdate outputs a human-readable date in the current locale, so parsing it is as useless as parsing apt install outputdate +%d-%b-%Y. Your code depends on the date being output as something like Sun Feb 21 2021 15:12:35, but my output has the time before the year (Sun Feb 21 15:12 GMT 2021), so I get 21-Feb-15:12:35.echo "`date "+%F"`" Will print YYYY-MM-DD
1 Comment
%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 enoughYou 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 Comment
backticks`` are obsolete and deprecated long ago. Never use them, use $() instead which is nestable and readableTry this code for a simple human readable timestamp:
dt=$(date) echo $dt Output:
Tue May 3 08:48:47 IST 2022
date -Iis all you need. It took me years to stumble upon that.date -Ionly works on Linux using GNU tools, not on macOS / BSD; installing brew -->brew install coreutilsmakesgdate -Iavailabledate -r <TIMESTAMP> -Iworks fine under the BSD tools FWIW.date -Inow works on macOS