1753828 Members
8512 Online
108806 Solutions
New Discussion юеВ

Find out day of the week

 
SOLVED
Go to solution
Franky_1
Respected Contributor

Find out day of the week

Hello @,

what is the easiest way to find out the appropraite day of the week (Mo,Tu,We..)of any given date in the past
e.g. 11/18/2005 = Friday

Thx in advance

Franky
Don't worry be happy
14 REPLIES 14
Steven E. Protter
Exalted Contributor

Re: Find out day of the week

Best tool arournd is A. Clay Stephenson's caljd series of scripts.

It comes in both perl and shell versions.

http://hpux.ws/merijn/caljd-2.23.sh

http://hpux.ws/merijn/caljd-2.2.pl

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Arunvijai_4
Honored Contributor

Re: Find out day of the week

Here is a great link to find it out,

http://mathforum.org/dr.math/faq/faq.calendar.html

[How do I find the day of the week for any date?] Zeller's Rule

-Arun

"A ship in the harbor is safe, but that is not what ships are built for"
Franky_1
Respected Contributor

Re: Find out day of the week

Hello,

thx for your fast replies

Steven :
How do i use the caljd program and how do i get the day of the week as an output from the caljd program alternatively ?

Thx

Franky
Don't worry be happy
Muthukumar_5
Honored Contributor

Re: Find out day of the week

You use this simply:

#!/bin/ksh
echo "Enter your date: Format DD/MM/YYYY"
read time;

date=$(echo $time|cut -d"/" -f1)
month=$(echo $time|cut -d"/" -f2)
year=$(echo $time|cut -d"/" -f3)

fno=$(cal $month $year | grep $date | awk -v var=$date '{ for (i=1;i<=NF;i++) { if ( $i ~ var ) { print i;}}}')

if [[ $fno -eq 1 ]]
then
echo "Given date of $time is Sunday"
elif [[ $fno -eq 2 ]]
then
echo "Given date of $time is Monday"
elif [[ $fno -eq 3 ]]
then
echo "Given date of $time is Tuesday"
elif [[ $fno -eq 4 ]]
then
echo "Given date of $time is Wednesday"
elif [[ $fno -eq 5 ]]
then
echo "Given date of $time is Thursday"
elif [[ $fno -eq 6 ]]
then
echo "Given date of $time is Friday"
elif [[ $fno -eq 7 ]]
then
echo "Given date of $time is Saturday"
fi

# end
exit 0

# ksh ./day.ksh
Enter your date: Format DD/MM/YYYY
10/12/2004
Given date of 10/12/2004 is Friday
# cal 12 2004
December 2004
S M Tu W Th F S
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31

It is saying it is working ;)
Easy to suggest when don't know about the problem!
Muthukumar_5
Honored Contributor
Solution

Re: Find out day of the week

with caljd-2.23.sh script,

# ./caljd-2.23.sh -w -O DD MM YYYY

Example:
# ./caljd-2.23.sh -w -O 11 12 2005
Saturday

-Muthu
Easy to suggest when don't know about the problem!
James R. Ferguson
Acclaimed Contributor

Re: Find out day of the week

Hi Franky:

#cat weekday
#!/usr/bin/perl -wl
use Time::Local;
$wday = (localtime(timelocal(0,0,0,$ARGV[1],$ARGV[0]-1,$ARGV[2]-1900))) [6];
print $wday;

Run as ./weekday 11 18 2005

...returns the numeric day of the week, 0-6 where 0=Sunday.

Regards!

...JRF...
Doug O'Leary
Honored Contributor

Re: Find out day of the week

Hey;

A slight modification to J.Fergeson's excellent script:

#!/usr/bin/perl

use strict;
use Time::Local;
my @days = qw(Sunday Monday Tuesday Wednesday Thursday Friday);

my ($mon, $day, $year) = split(/\//, $ARGV[0]);
my $wday = (localtime(timelocal(0,0,0,$day, $mon-1, $year%100)))[6];
print "$days[$wday]\n";


That way, you execute:

$ ./weekday 12/10/2004
Friday


------
Senior UNIX Admin
O'Leary Computers Inc
linkedin: http://www.linkedin.com/dkoleary
Resume: http://www.olearycomputers.com/resume.html
Geoff Wild
Honored Contributor

Re: Find out day of the week

And another way using built in cal:

# !/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

Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Doug O'Leary
Honored Contributor

Re: Find out day of the week

Sorry; fingers got ahead of me there...

my $wday = (localtime(timelocal(0,0,0,$day, $mon-1, $year%100)))[6];

should be:

my $wday = (localtime(timelocal(0,0,0,$day, $mon-1, 1900+$year%100)))[6];


------
Senior UNIX Admin
O'Leary Computers Inc
linkedin: http://www.linkedin.com/dkoleary
Resume: http://www.olearycomputers.com/resume.html