# Get a week number from a date.

ibm aix / 09
November 17, 2009 at 00:48:09
Specs: UNIX, na
 Hi frnds, I donno if I amputting in the rt forum or not.But i am trying to get a week number from a given date. This code by a member on computing.net, is executed, but doesnot give the exact output.function figureWeek{today=\$(date +%j) #displays the day of the year as decimal (001-366)(( theWeek = ((( today - \$# ) / 7) + 1 ) )) #What is the theWeek??print "This is week \$theWeek"}year=\$(date +%Y)counter=0cal 01 \$year | while read linedoif [[ \$counter = 2 ]]thenfigureWeek \$linebreakelse(( counter += 1 ))fidoneBut the code above gives the O/P week number which is 46. I am not good in PERL.I use UNIX AIX.Can anybody help with the right code for ISO week number.Thanks in advanceRaj.

See More: Get a week number from a date.

#1
November 17, 2009 at 08:08:59

Report •

#2
November 25, 2009 at 03:49:43
 The problem is that you need to figure out if the week of January 1st is less than 7 days long. I'm tired and could probably do this cleaner, but I simply added a function to determine if the first week of the year has less than 7 days. If so, increment week by 1. Otherwise, don't.#!/bin/kshfunction figureOffSet{integer counter=0cal 01 \$year | while read linedoif [[ \$counter -eq 2 ]] && [[ \${#line} -eq 19 ]]then offset=7fi(( counter +=1 ))done}function figureWeek{today=\$(date +%j) #displays the day of the year as decimal (001-366)(( theWeek = (((( today - ( offset) ) - \$1 ) / 7) + 1 ) ))if [[ \$offset -ne 7 ]]then (( theWeek = theWeek + 1 ))fiprint "This is week \$theWeek"}year=\$(date +%Y)counter=0cal 01 \$year | while read linedoif [[ \$counter = 2 ]]thenfigureOffSetfigureWeek \$linebreakelse(( counter += 1 ))fidone

Report •

#3
October 28, 2010 at 02:33:11
 The best way is to use the following command.date -d "7 days ago" "+%Y-%m-%d"

Report •

Related Solutions

#4
October 30, 2010 at 20:27:49
 He is trying to get the week number, not the date minus 7 days. Your command would return:2010-10-23if run today (Oct 30, 2010). How does he get the week of the year number from that command? Additionally, that format only works with the GNU date command. I'm not sure of other Unix versions, but it doesn't work on AIX.

Report •