How to get the weekday of the given date?

Hi All,
Thanks in Advance.

I want a function/script which returns the weekday of the given date.
the input to the function/script is the date with format MM/DD/YYYY, it should return the weekday as 1 for sunday, 2 for monday .......7 for saturday.

ex: if the function called like this date_func 12/15/2005
it should return 5 (thursday)

your help will be greatly appreciated

Regards,
Rinku

Look at datecalc

one more way,

not thoroughly tested

>cat weekday
# !/usr/bin/bash

weekday=$1

month=`echo ${weekday:0:2}`
day=`echo ${weekday:3:2}`
year=`echo ${weekday:6:4}`
weekday=1

for dayval in `cal $month $year | grep $day`
do
 if [ $day -ne $dayval ]
 then
   weekday=$(($weekday + 1))
 else
   break
 fi
done

echo "weekday is $weekday"

exit 0

hi,

just saw the datecalc...was checking the julian day option for the same.If i want to find todays julian day using "date" command the output is:

$date +%j
$350

but if i use datecalc for calculating todays julian day:

$datecalc -j 2005 12 16
$53720

just wondering what the output means here :confused:

would be great if someone can throw some light on the same :slight_smile:

regards,
Bhups

First of all,

"The Julian day or Julian day number (JDN) is the number of days that have elapsed since 12 noon Greenwich Mean Time (UT or TT) on Monday, January 1, 4713 BC in the proleptic Julian calendar."

datecalc is using the modified julian day number:

"The Modified Julian Day (MJD), introduced by the Smithsonian Astrophysical Observatory in 1957 to record the orbit of Sputnik, is defined in terms of the Julian day as follows: MJD = JD - 2400000.5 The offset of 0.5 means that MJD started at midnight at the beginning of November 17, 1858, and that every Modified Julian Day begins and ends at midnight UT or TT."

The author of the date program you are using has confused day-of-year with julian day:

"The use of Julian date to refer to the day-of-year (ordinal date) is usually considered to be incorrect."

hi Perderabo,

thanks for the info :slight_smile: ...i really use to think that julian day refer to (what you say) day-of-year...

so my next obvious question :stuck_out_tongue: is how to get day-of-year from datecalc??

in my scripts i always come across this prob of converting julain date (CCYY DDD) to the MM DD YY format, so it would be a great help if i could use datecalc for the same purpose :cool:

thanks in advance...

regards,
Bhups.

Should Jan 1 be a zero or a one? Both schemes are in use.

Compute the mjd of January 1 for the year. (Should Jan 1 be 1? If so subtract one.) Subtract the result from the mjd of the date to get day-of-year.

Hi all,

Thank you very much for your prompt responses.
I got some solution in the net its working fine in my desktop(windows 2000) with cygwin, but its giving bailing error in my server(Sun OS 5.8).
Could any one of you help me to solve the problem, the code is below.

#!/bin/ksh

dte='2003-06-11'

eval $(echo "${dte}" | awk -F- '{printf("year=%s month=%s day=%s\n", $1, $2, $3)}')

echo "year->[${year}] month->[${month}] day->[${day}]"

cal "${month}" "${year}" | awk -v day="${day}" '
FNR > 2 {
for(i=1; i <= NF; i++)
if ( $i == day) {
#printf("day->[%d] row->[%d]\n", $i, FNR-2)
printf("%d\n", (NF == 7 || FNR!=3) ? i-1 : i+(6-NF))
exit
}
}
'

Thanks in Advance
Rinku

use 'nawk' instead of 'awk' on Solaris.

thanks Perderabo....datecalc is the real answer to all date manipulations....

I wonder : Is there a way in a KSH shell what the yesterdays last business day is:
so for instance monday 03112008 (ddmmyyyy) should be give 31102008.
I've search this site but couldn't found something. Did also tried with Julians Day

I'm using AIX 5.2.0 with KSH shell pls give me a hand

$ cat yesterbusinessday
#! /usr/bin/ksh

alias datecalc=./datecalc
echo "Enter year month day"
read year month day

if (($(datecalc -d $year $month $day) == 1)) ; then
        offset=3
else
        offset=1
fi
datecalc -j $(($(datecalc -j $year $month $day) - offset))
exit 0
$ ./yesterbusinessday
Enter year month day
2008 11 3
2008 10 31
$ ./yesterbusinessday
Enter year month day
2008 10 31
2008 10 30
$

thx Perderabo for the fast reply ,

but i got a problem with the
# ./yesterbusinessday
+ alias datecalc=./datecalc
+ echo Enter year month day
Enter year month day
+ read year month day
2008 11 3
+ ./datecalc -d 2008 11 3
./yesterbusinessday[7]: ./datecalc: not found.
+ (( == 1 ))
./yesterbusinessday[7]: == 1: 0403-057 Syntax error
:confused:
Sorry i'm new

I don't have an AIX ksh to test so I can't help you there. As for datecalc, the second post in this thread links to it.

:b: Thx m8 if u combine those 2 it works I'm happy

Small question:
Is it also possible to get the prev bussinees day back like yyyy mm dd instead of yyyy mm d ?