问题
From a given date in %m-%d-%Y format we should determine what day it is Please help me with the command
like if i pass a date like 09-01-2017 output should be Friday
回答1:
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
回答2:
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
回答3:
Very simple. Just use the date command itself with correct options.
$ date -j -f '%m-%d-%Y' "09-01-2017" +'%A'
Friday
来源:https://stackoverflow.com/questions/46024251/how-can-we-get-weekday-based-on-given-date-in-unix