YYYY-MM-DD format date in shell script - bash

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

Try: $(date +%F)
The %F option is an alias for %Y-%m-%d

You can do something like this:
$ date +'%Y-%m-%d'

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

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)

date -d '1 hour ago' '+%Y-%m-%d'
The output would be 2015-06-14.

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.

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

if you want the year in a two number format such as 17 rather than 2017, do the following:
DATE=`date +%d-%m-%y`

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

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

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.

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

echo "`date "+%F"`"
Will print YYYY-MM-DD

Try this code for a simple human readable timestamp:
dt=$(date)
echo $dt
Output:
Tue May 3 08:48:47 IST 2022

Related

bash - command in a variable - issue with quotes

I want to store the date of last Sunday in a variable.
#/bin/bash
OFFSET=$(date +%u)
COMMAND="date --date='"$((OFFSET))" days ago' +%Y%m%d"
DATEFULL=$($COMMAND)
echo offset $OFFSET
echo command $COMMAND
echo datefull $DATEFULL
Output:
[root#localhost ~]# ./test.sh
date: extra operand ‘ago'’
Try 'date --help' for more information.
offset 1
command date --date='1 days ago' +%Y%m%d
datefull
Pasting the result of $COMMAND in command line gives the right result:
[root#localhost ~]# date --date='1 days ago' +%Y%m%d
20210822
So why does he claim about "date: extra operand ‘ago'’" ? How can I execute the result of command and set it in a variable?
Try to use arrays for commands, instead of variables :
#!/bin/bash
OFFSET=$(date +%u)
COMMAND=(date "--date=$OFFSET days ago" +%Y%m%d)
DATEFULL=$("${COMMAND[#]}")
echo offset $OFFSET
echo command $COMMAND
echo datefull $DATEFULL
Also all uppercase variable names can cause clashes with bash-defined variables.
I'm unsure why this doesn't work. I think it has something to do with bash parsing quotes out of the command. If you use eval, it works:
#/bin/bash
OFFSET=$(date +%u)
COMMAND="date --date='"$((OFFSET))" days ago' +%Y%m%d"
DATEFULL=$(eval "$COMMAND")
echo offset $OFFSET
echo command $COMMAND
echo datefull $DATEFULL
Output
$ OFFSET=$(date +%u)
$ COMMAND="date --date='"$((OFFSET))" days ago' +%Y%m%d"
$ DATEFULL=$(eval "$COMMAND")
$ echo offset $OFFSET
offset 1
$ echo command $COMMAND
command date --date='1 days ago' +%Y%m%d
$ echo datefull $DATEFULL
datefull 20210822

What are shell command to get current date and date 3 hrs back?

To get the current command we simple shell script command
date
if you want to get a custom date format
NOW=$(date +"%Y-%m-%d %T")
Am not getting simple command to get the 3hrs back date with above format , any one can help me please .
i used below shell command with perl like below , but i do not want another additional dependency perl , i just need to shell command
CURRENT_DATE=`perl -e 'use POSIX qw(strftime);$d = strftime "%Y-%m-%d %H:%M:%S", localtime(time());print $d'`
CURRENT_DATE_MINUS_3_HRS_DATE=`perl -e 'use POSIX qw(strftime);$d = strftime "%Y-%m-%d %H:%M:%S", localtime(time() - 10800);print $d'`
The date command has the nice keyword ago for the date you can give with -d option:
date -d '3 hours ago' +"%Y-%m-%d %T"
Shows the current date:
date "+%Y-%m-%d %T"
Show date back to 3 hours:
date -d "-3 Hours" "+%Y-%m-%d %T"
Some of the other things you can try:
# Back to 3 days
date -d "-3 Days" "+%Y-%m-%d %T"
# Next to 3 days
date -d "3 Days" "+%Y-%m-%d %T"
# Back to 2 Years and 3 Hours
date -d "-2 Years -3 Hours" "+%Y-%m-%d %T"
Using the BSD date that ships with macOS, use the -v option.
$ date +"%Y-%m-%d %T"
2018-11-08 16:25:21
$ date -v -3H +"%Y-%m-%d %T"
2018-11-08 13:25:26

How can we get weekday based on given date in unix

From a given date in %m-%d-%Y format we should determine what day it is.
Example: for the date 09-01-2017 output should be Friday
Very simple. Just use the date command itself with correct options.
$ date -j -f '%m-%d-%Y' "09-01-2017" +'%A'
Friday
If you have your date like this:
d="09-01-2017"
you need to reformat it to "YYYY-MM-DD"
date -d $(echo $d|awk -F- '{print $3 "-" $1 "-" $2}') +%A # DOW
Here is what I usually do. I would use the date function.
you can do 'man date' and find options.
$ d=2020-08-20 \
$ date -d "$d" +%u \
4 \
$ date -d "$d" +%A \
Thursday
DayOfWeek=$(date +%A)
This would yield the day of week monday-sunday
If your input date is strictly in the format MM-DD-YYYY, use the following
IFS='-' read -ra ADDR <<< "09-01-2017"
formattedDate=${ADDR[2]}-${ADDR[0]}-${ADDR[1]}
date -d $formattedDate +%A
The first line tokenizes the components of the date and the second rearranges them
You can pass it as %m/%d%Y which gets recognized by the date command.
$ date --date="`echo 09-01-2017| sed -e 's/-/\//g' `" +'%A'
Friday
To verify it, pass %F to get it in ISO format
$ date --date="`echo 09-01-2017| sed -e 's/-/\//g' `" +'%A %F'
Friday 2017-09-01
date +%A
# let us see in a for loop
for i in {1..7}; do date +%A --date=+${i}day; done
Wednesday
Thursday
Friday
Saturday
Sunday
Monday
Tuesday

Shell Script for Yesterdays Date

I have tried adding -d "yesterday" but I haven't had any luck getting it to work. Here is what I have for the whole script:
#! /bin/bash
saveDir="TJ"
dd=$(date +"%m-%d-%Y")
for file in *.csv ; do
saveName="${saveDir}/TJ ${dd}.csv"
cut -d',' -f2,14 "$file" > "$saveName"
done
how do I get dd to output yesterdays date instead of the current date?
EDIT: This is what I have now
#! /bin/bash
saveDir="TJ"
dd=$(date --date='yesterday' +'%m-%d-%Y')
for file in *.csv ; do
saveName="${saveDir}/TJ ${dd}.csv"
cut -d',' -f2,14 "$file" > "$saveName"
done
the above is saving the file as TJ .csv but I'm not sure what was done incorrectly
I think you want to use -
$ cat test.sh
#!/bin/bash
dd=$(date --date='yesterday' +'%m-%d-%Y')
echo $dd
$ ./test.sh
12-31-2013
or you could use
$ date -d '1 day ago' +'%m-%d-%Y'
12/31/2013
And for tomorrow -
$ date -d '1 day' +'%m-%d-%Y'
01/02/2014
or
$ date --date='tomorrow'
Thu Jan 2 21:25:00 EST 2014
Get today's date in seconds since epoch. Subtract 86400 to get to yesterday. Then convert yesterday to the string format you want.
today=`date +"%s"`
yesterday=`expr $today - 86400`
dd=`date --date="#${yesterday}" +"%m-%d-%Y"`
Try this
yday=$(date --date yesterday "+%d-%m-%Y")
echo $yday
And If you works in Linux
yday=$(date -d "-1 days" +"%d-%m-%Y")
echo $yday
I tried date -d "yesterday" +%m-%d-%Y on my Linux, it worked fine.
If you are on unix platform, you cannot use -d,
you can get yesterday's date using perl, this is how I do using perl
dd=$(perl -e '($a,$b,$c,$day,$mon,$year,$d,$e,$f) = localtime(time-86400);printf "%02d-%02d-%4d",$day, $mon+1, $year+1900')
echo $dd
01-01-2014
NewDate=`date +"%A %d %B %Y" --date="-1 day"`
echo $NewDate
this will give your yesterday's date (-1)
This will give you tomorrow's date (+1)
even you can check for any values like (+/-) days

Get the date (a day before current time) in Bash

How can I print the date which is a day before current time in Bash?
if you have GNU date and i understood you correctly
$ date +%Y:%m:%d -d "yesterday"
2009:11:09
or
$ date +%Y:%m:%d -d "1 day ago"
2009:11:09
If you have BSD (OSX) date you can do it like this:
date -j -v-1d
Wed Dec 14 15:34:14 CET 2011
Or if you want to do date calculations on an arbitrary date:
date -j -v-1d -f "%Y-%m-%d" "2011-09-01" "+%Y-%m-%d"
2011-08-31
date --date='-1 day'
MAC OSX
For yesterday's date:
date -v-1d +%F
where 1d defines current day minus 1 day. Similarly,
date -v-1w +%F - for previous week date
date -v-1m +%F - for previous month date
IF YOU HAVE GNU DATE,
date --date="1 day ago"
More info: https://www.cyberciti.biz/tips/linux-unix-get-yesterdays-tomorrows-date.html
Sorry not mentioning I on Solaris system.
As such, the -date switch is not available on Solaris bash.
I find out I can get the previous date with little trick on timezone.
DATE=`TZ=MYT+16 date +%Y-%m-%d_%r`
echo $DATE
Well this is a late answer,but this seems to work!!
YESTERDAY=`TZ=GMT+24 date +%d-%m-%Y`;
echo $YESTERDAY;
Advanced Bash-scripting Guide
date +%Y:%m:%d -d "yesterday"
For details about the date format see the man page for date
date --date='-1 day'
date -d "yesterday" '+%Y-%m-%d'
or
date=$(date -d "yesterday" '+%Y-%m-%d')
echo $date
Use Perl instead perhaps?
perl -e 'print scalar localtime( time - 86400 ) . "\n";'
Or, use nawk and (ab)use /usr/bin/adb:
nawk 'BEGIN{printf "0t%d=Y\n", srand()-86400}' | adb
Came across this too ... insane!
/usr/bin/truss /usr/bin/date 2>&1 | nawk -F= '/^time\(\)/ {gsub(/ /,"",$2);printf "0t%d=Y\n", $2-86400}' | adb
date --date='-1 day'
Not very sexy but might do the job:
perl -e 'my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time - 86400);$year += 1900; $mon+= 1; printf ("YESTERDAY: %04d%02d%02d \n", $year, $mon, $mday)'
Formated from "martin clayton" answer.
You could do a simple calculation, pimped with an regex, if the chosen date format is 'YYYYMM':
echo $(($(date +"%Y%m") - 1)) | sed -e 's/99$/12/'
In January of 2020 it will return 201912 ;-)
But, it's only a workaround, when date does not have calculation options and other dateinterpreter options (e.g. using perl) not available ;-)
short answer (GNU format):
date +%Y-%m-%d -d "-2 day"
if you are using OSX, but you need create for GNU compatible, install coreutils first
brew install coreutils
then edit your profile with:
#gnu coreutils first
export PATH="/usr/local/opt/coreutils/libexec/gnubin:$PATH"
re-start your terminal, and now you able to use GNU format!
yesterday=`date -d "-1 day" %F`
Puts yesterday's date in YYYY-MM-DD format into variable $yesterday.
#!/bin/bash
OFFSET=1;
eval `date "+day=%d; month=%m; year=%Y"`
# Subtract offset from day, if it goes below one use 'cal'
# to determine the number of days in the previous month.
day=`expr $day - $OFFSET`
if [ $day -le 0 ] ;then
month=`expr $month - 1`
if [ $month -eq 0 ] ;then
year=`expr $year - 1`
month=12
fi
set `cal $month $year`
xday=${$#}
day=`expr $xday + $day`
fi
echo $year-$month-$day
DST aware solution:
Manipulating the Timezone is possible for changing the clock some hours. Due to the daylight saving time, 24 hours ago can be today or the day before yesterday.
You are sure that yesterday is 20 or 30 hours ago. Which one? Well, the most recent one that is not today.
echo -e "$(TZ=GMT+30 date +%Y-%m-%d)\n$(TZ=GMT+20 date +%Y-%m-%d)" | grep -v $(date +%Y-%m-%d) | tail -1
The -e parameter used in the echo command is needed with bash, but will not work with ksh. In ksh you can use the same command without the -e flag.
When your script will be used in different environments, you can start the script with #!/bin/ksh or #!/bin/bash. You could also replace the \n by a newline:
echo "$(TZ=GMT+30 date +%Y-%m-%d)
$(TZ=GMT+20 date +%Y-%m-%d)" | grep -v $(date +%Y-%m-%d) | tail -1
Try the below code , which takes care of the DST part as well.
if [ $(date +%w) -eq $(date -u +%w) ]; then
tz=$(( 10#$gmthour - 10#$localhour ))
else
tz=$(( 24 - 10#$gmthour + 10#$localhour ))
fi
echo $tz
myTime=`TZ=GMT+$tz date +'%Y%m%d'`
Courtsey Ansgar Wiechers
date +%Y:%m:%d|awk -vFS=":" -vOFS=":" '{$3=$3-1;print}'
2009:11:9

Resources