How can we get weekday based on given date in unix
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
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