The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
calculating sum of fields in a file dsravan Shell Programming and Scripting 4 01-03-2008 01:00 PM
Calculating quantiles in SQL figaro UNIX for Dummies Questions & Answers 3 11-20-2007 05:55 PM
Calculating the average kekanap Shell Programming and Scripting 2 01-26-2007 09:36 AM
calculating size of int apoorvasharma80 High Level Programming 10 11-01-2006 06:44 PM
How to identify week-1 and week-2 csaha Shell Programming and Scripting 1 02-16-2006 05:24 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-14-2001
ligs ligs is offline
Registered User
  
 

Join Date: Oct 2001
Posts: 5
Question Calculating the day of the week

Hi all,

I would like to calculate the day of the week using a supplied date.

i.e. 20011012 = Day 5.

Any ideas?

Many thanks,

ligs
  #2 (permalink)  
Old 10-15-2001
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,111
Here is a ksh script:
Code:
#! /usr/bin/ksh

#              ja fe ma ap ma ju ju ag se oc no de
set -A lasts 0 31 28 31 30 31 30 31 31 30 31 30 31


year=$1
month=$2
day=$3

#
# Get Day of Week of Jan 1
dow1=$(cal 1 $year | sed -n '3s/. //gp')
((dow1=7-dow1))

#
#  is this a leap year?
leap=0
if ((!(year%100))); then
     ((!(year%400))) && leap=1
else
     ((!(year%4))) && leap=1
fi

#
#  set number of days in february
lasts[2]=28
((leap)) && lasts[2]=29

#
#  calculate day of year
i=0
previous=0
while ((i < month)) ; do
     ((previous=previous+lasts[i]))
     ((i=i+1))
done
((doy=previous+day))

#
#  Calculate day of week
((dow = (doy+dow1-1)%7 ))

echo dow = $dow

exit 0
  #3 (permalink)  
Old 10-15-2001
ligs ligs is offline
Registered User
  
 

Join Date: Oct 2001
Posts: 5
Thanks a lot Perderabo

I'll give it a go.


Ligs
  #4 (permalink)  
Old 10-24-2001
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,111
I knew there was an easier way if I just just a little more clever with the math. I finally got it. Hope you're still reading, ligs, this is much faster...
Code:
#! /usr/bin/ksh
year=$1
month=$2
day=$3

#
#  Get date of last day in 1st week of month
dldw1=$(cal $month $year | sed -n '3s/. //gp')

#
#  Calculate day of week
((dow = (day+dldw1+1)%7 ))

echo dow = $dow
exit 0
  #5 (permalink)  
Old 10-27-2001
ligs ligs is offline
Registered User
  
 

Join Date: Oct 2001
Posts: 5
Hi Perderabo,

I'll try this one too.

Your last piece of code worked a treat, thanks a lot.

Ligs
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 09:17 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0