Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I was wondering if there was an easy way to use cal with maybe either awk or sed to easily give the user the day of the week for the given day they entered (assuming the date was valid).
Is that possible, or do I have to sit there and do all kinds of calculations to get the correct day?
Thanks.

I found a script I wrote (datestamped Apr 2002), that uses cal and awk to determine DOW.
# !/bin/ksh
mmddyy=`echo $1 | tr "[/]" "[ ]"`
echo $mmddyy | read mo day yr junk
day=${day#0}cal $mo $yr | awk 'BEGIN {
day="'$day'"
split("Su Mo Tu We Th Fr Sa Su",DOW)
getline
getline}
{if (col=index($0,day))
{dow=int((col+2)/3)
print "DOW=" DOW[dow
print "DOW=" DOW[dow]
exit}
}'exit 0
./dow.sh 03/23/1947
DOW=Su

I had a little posting error. Here is the proper code:
# !/bin/ksh
mmddyy=`echo $1 | tr "[/]" "[ ]"`
echo $mmddyy | read mo day yr junk
day=${day#0}cal $mo $yr | awk 'BEGIN {
day="'$day'"
split("Su Mo Tu We Th Fr Sa Su",DOW)
getline
getline}
{if (col=index($0,day))
{dow=int((col+2)/3)
print "DOW=" DOW[dow]
exit}
}'exit 0
./datedow.sh 03/23/1947
DOW=Su

thanks for the post,
quick question though, I am new to UNIX in general, and only know how to use bash. It looks as though that is for ksh. If it is not too much trouble, do you know what it would look like in bash. Thanks again.

On some linux servers, ksh is an alias for bash, so try it just like it is first. If it does not like the ksh, change ksh to bash and try that.

I tried it just as is, and with changing it to bash instead of ksh, and whenever it is run, it just goes back to the prompt, no result, nothing displayed, not even an error. Also, I am using sun solaris unix, not linux. Any ideas...?
Thanks.

If I use sh datedow.sh 03/23/1947, then i get the error:
"datedow.sh: bad substitution"
and like I said, if i use just datedow.sh 03/23/1947 I get nothing.

This is a shell implementation of Mike Keith's date algorithm:
#!/bin/bash
# This function returns the day of week where
# 0 = sunday, 1 = monday, ... 6 = saturday
# This algorithm is from Mike Keith's World of Words & Numbers:
# http://users.aol.com/s6sj7gt/mikecal.htm
# arguments: $1 = month, $2 = day, $3 = year in format YYYY
get_dow()
{
# [.] means to truncate downward to the nearest integer
# dayofweek=([23m/9] + d + 4 + y + [z/4] - [z/100] + [z/400] - 2 (if m >= 3)) mod 7
typeset -i DOW
typeset -i z
typeset -i multprif [ $1 -lt 3 ]
then # year determination
z=$(($3-1))
else
z=$3
fi
if [ $1 -ge 3 ]
then # set the multiplier
multpr=2
else
multpr=0
fi
DOW=$(( ((23 * $1/9) + $2 + 4 + $3 + ($z/4) - ($z/100) + ($z/400) - $multpr) % 7))echo $DOW
}# return a day string
# sunday, monday, .. saturday
get_day_string()
{
case $1 in
0) echo "sunday";;
1) echo "monday";;
2) echo "tuesday";;
3) echo "wednesday";;
4) echo "thursday";;
5) echo "friday";;
6) echo "saturday";;
*) echo "error";;
esac
}mydate="03/27/1947"
set - $(IFS='/'; echo $mydate)
TY=$3
TM=$1
TD=$2day_str=$(get_day_string $(get_dow $TM $TD $TY) )
echo $day_str # should echo thursday

The problem is going to be with the day= command. I have to get any leading zero removed from the day portion of the date argument so that it will be an exact match against the day in the cal output which does not have leading zeros.
In the code below, I now use an expr command to strip any leading zeros, so I think that should work for you. For testing only, I left the old command in also (as dayx), and I display values for dayx and day just to confirm what substitutions are being made. You can remove both of the lines that contain dayx.
mmddyy=`echo $1 | tr "[/]" "[ ]"`
echo $mmddyy | read mo day yr junk
dayx=${day#0}
day=`expr $day : "0*\(.*\)"`
echo "TEST: dayx=$dayx day=$day"cal $mo $yr | awk 'BEGIN {
day="'$day'"
split("Su Mo Tu We Th Fr Sa Su",DOW)
getline
getline}
{if (col=index($0,day))
{dow=int((col+2)/3)
print "DOW=" DOW[dow]
exit}
}'

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |