Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 860
Divya S Avatar asked Sep 03 '17 14:09

Divya S


People also ask

What is date %s in Unix?

%r – Displays 12-hour clock time (01-12) using the AM-PM notation; in the POSIX locale, it is equivalent to %I:%M:%S %p. %S – Displays the seconds as a decimal number (00 – 59). %T – Displays the 24-hour clock (00-23) in the format equivalent to HH:MM:SS.


2 Answers

Very simple. Just use the date command itself with correct options.

$ date -j -f '%m-%d-%Y' "09-01-2017" +'%A'
Friday
like image 170
nagendra547 Avatar answered Sep 28 '22 09:09

nagendra547


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 
like image 27
Juan Manuel García Avatar answered Sep 28 '22 09:09

Juan Manuel García