Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I know the command to get the week number of a date is date +%W

Output: 03

Now, is there a way of giving a week number it will display all the dates in that week? Or should I write a script?

Eg:

Input: 03

Output:

Jan 20
Jan 21
Jan 22
Jan 23
Jan 24
Jan 25
Jan 26

First week (Jan 1, 2, 3,4 and 5) are considered as week no '0'. So the first week starts from 6th jan(First Monday in Jan 2014), 2nd week starts from 13th and third week starts from 20th

share|improve this question

closed as unclear what you're asking by Anthon, Zelda, rahmu, slm, peterph Jan 21 at 13:48

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question.If this question can be reworded to fit the rules in the help center, please edit the question.

    
What platform are you running on? uname -a –  Kevdog777 Jan 21 at 9:17
    
Your week has only 6 days? And the week number seems not to be based on the ISO standard en.wikipedia.org/wiki/ISO_week_date, what standard do you follow for numbering? –  Timo Jan 21 at 9:43
    
What distro? Do you have ncal? –  slm Jan 21 at 9:44
    
Hi its x86_64 GNU/Linux –  Rakesh K Jan 21 at 10:12
3  
The ISO week has 7 days starting from Monday as well. But ISO week 03 of 2014 starts on Jan 13th, so again what standard or pattern are you following? Please clarify why this is week #3 and not #4 as with ISO –  Timo Jan 21 at 10:58
show 5 more comments

2 Answers 2

up vote 1 down vote accepted

Cheers, I was going to put a comment to point you to this stackoverflow question but since my reputation doesn't allow it I decided :-) to rip it and further adapt it to your question:

#!/bin/bash
week=$1 year=$2
date_fmt="+%b %d"

week_num_of_Mon_1=$(date -d $year-01-01 +%W)
week_day_of_Mon_1=$(date -d $year-01-01 +%u)

if ((week_num_of_Mon_1)); then
   first_Mon=$year-01-01
else
   first_Mon=$year-01-$((01 + (7 - week_day_of_Mon_1 + 1) ))
fi

for nr_of_day_of_week in 0 1 2 3 4 5 6
do
    day_of_week=$(date -d "$first_Mon +$((week - 1)) week + $nr_of_day_of_week day" "$date_fmt")
    echo $day_of_week
done

Example: weekof 17 2014 Result:

Apr 28
Apr 29
Apr 30
May 01
May 02
May 03
May 04

I hope it helps!

share|improve this answer
    
Please note that is not the ISO standard week number. It is the week starting with the 17th Monday in 2014. –  Timo Jan 21 at 9:49
    
Thanks Timo.. So basically we need a script .. can not be done in command line –  Rakesh K Jan 21 at 10:14
add comment

ncal -w gives you week numbers and you could parse the output using awk.

What I found easier is install the python-dateutil package and use:

import sys, datetime
from dateutil.relativedelta import relativedelta, MO

week = int(sys.argv[1])
if len(sys.argv) == 2:
    year = datetime.date.today().year
else:
    year = int(sys.argv[2])

beginning = datetime.date(year, 1, 1) + \
    relativedelta(day = 4, weekday = MO(-1), weeks = week-1)

for i in range(7):
    print beginning
    beginning += datetime.timedelta(1)

This gives you the 7 days based on the ISO week number.

share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.