Operating System - HP-UX
1752822 Members
4385 Online
108789 Solutions
New Discussion юеВ

Getting the day from a given date

 
Enda Martin_1
Occasional Contributor

Getting the day from a given date

Hi,
is there a way to calculate the day if I've got a generated date.

For example :

If I have 030121 as a date, how do I calculate that the associated day is Tue ?

Cheers,
Enda
8 REPLIES 8
Julio Yamawaki
Esteemed Contributor

Re: Getting the day from a given date

Enda,

It's possible to calculte the day of the week in a C routine, COBOL or any other language.
Using Posix Shell I think it's not possible.
Do you need a routine in any of the above Language?
Patrick Wallek
Honored Contributor

Re: Getting the day from a given date

I think this can be done using A. Clay Stephensons caljd.sh or caljd.pl scripts.

To find them go to: http://www.cmve.net/~merijn/ and scroll all the way to the bottom to the section labeled "A. Clay Stephensons Date Hammer" and download either the shell version or perl version and make sure you chmod them so that they are executable.

To get full usage instructions do:

# ./caljd.sh -u

or

# ./caljd.pl -u
A. Clay Stephenson
Acclaimed Contributor

Re: Getting the day from a given date

It's actually easy. The one problem is that the routine attached will assume with your date that it's year 21 AD,
so

WKDAY=$(caljd.sh -w -o $(caljd.sh -y 2003 01 21))
echo "Day of the week: ${WKDAY}"

If you want a numerical value then drop the -w for 0 -> Sun; 6-> Sat. If you change -o to -O Tue becomes Tuesday and it also speaks other languages if you set the LOCALE.

Invoke as caljd.sh -u for full usage and many examples.

If it ain't broke, I can fix that.
A. Clay Stephenson
Acclaimed Contributor

Re: Getting the day from a given date

Oops, I meant caljd.sh will assume that's it's 3AD not 21AD.
If it ain't broke, I can fix that.
Thom Cornwell
Frequent Advisor

Re: Getting the day from a given date

#!/bin/ksh
read Date?"Enter date: "
Month=$(echo ${Date}|cut -c3-4)
Day=$(echo ${Date}|cut -c5-6)
Year=$(echo ${Date}|cut -c1-2)
Year=$(echo 20${Year})
cal ${Month} ${Year}
cal ${Month} ${Year}|
awk -vVar=${Day} '
{
split($0,ar," ")
if (ar[1] == Var) {print "Sunday"}
if (ar[2] == Var) {print "Monday"}
if (ar[3] == Var) {print "Tuesday"}
if (ar[4] == Var) {print "Wednesday"}
if (ar[5] == Var) {print "Thursday"}
if (ar[6] == Var) {print "Friday"}
if (ar[7] == Var) {print "Saturday"}
}'
John Wright_1
Advisor

Re: Getting the day from a given date

Hi,

This works.

Cheers,
JW.
John Wright_1
Advisor

Re: Getting the day from a given date

Hi,

The attachment works, but you'll need to change your date format.

Cheers,
JW.
Ian Dennison_1
Honored Contributor

Re: Getting the day from a given date

Does your routine return the date value as the Epoch (number of seconds since 1 jan 1970)?

Divide the number of seconds since the epoch by 86400 (the number of seconds in a day),
then by 7. multiply the integer answer by 7 and subtract from the original number of days.

The number you have been returned will be the number of days to add to the Epoch Date, so if the value returned is 1, and the Epoch was on a Sunday, your day value is Monday (you can set up an array for returning the day name).

Just be aware, you will have to amend the epoch value for your TimeZone.

Share and Enjoy! Ian
Building a dumber user