How can we get weekday based on given date in unix

痴心易碎 提交于 2020-03-18 17:54:16

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!