![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| 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 !! |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| calculating sum of fields in a file | dsravan | Shell Programming and Scripting | 4 | 01-03-2008 10:00 AM |
| Calculating quantiles in SQL | figaro | UNIX for Dummies Questions & Answers | 3 | 11-20-2007 02:55 PM |
| Calculating the average | kekanap | Shell Programming and Scripting | 2 | 01-26-2007 06:36 AM |
| calculating size of int | apoorvasharma80 | High Level Programming | 10 | 11-01-2006 03:44 PM |
| How to identify week-1 and week-2 | csaha | Shell Programming and Scripting | 1 | 02-16-2006 02:24 AM |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
#1
|
|||
|
|||
|
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 |
| Forum Sponsor | ||
|
|
|
#2
|
||||
|
||||
|
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
|
|||
|
|||
|
Thanks a lot Perderabo
I'll give it a go. Ligs |
|
#4
|
||||
|
||||
|
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
|
|||
|
|||
|
Hi Perderabo,
I'll try this one too. Your last piece of code worked a treat, thanks a lot. Ligs |
|||
| Google The UNIX and Linux Forums |
| Thread Tools | |
| Display Modes | |
|
|