Last sunday of current date

Hi

Please help me with this problem

i need to find the date of last week sunday from the current given date

Assuming you want the last sunday of the current month:

cal | awk 'NR==1{$1=$1;m=$0}$1{d=$1}END{print d, m}'

this is what are you looking for ? Take month and year separate from you date.

cal $mon $year | awk -v v1=$mon -v v2=$year 'NF>1{a=$1} END{print a v1 v2}'

hiii

first of all thanks a lot for reply

my question is

suppose my week starts from

sunday to saturday

so i am on any given date it should return me the last week sunday date

please help me on this

Post your input date format.

hiii
the input date format should be in year month day

like 20110314

Try this,

mon=$(echo "20110314"|cut -c5-6)
year=$(echo "20110314"|cut -c1-4)
cal $mon $year  | awk -v v1=$mon -v v2=$year 'NF>1{a=$1} END{print a v1 v2}'
27032011

hiii

thanks a lot all of ur reply

can u people please answer one more question

suppose i have week no , and i know day should be monday and i know year too

hw can i find the exact date of monday

please help me with this

To bring little tweak to Pravin code :

set -- $(echo "20110314" | sed 's/\(..\)..$/ \1/')
cal $2 $1  | awk -v m=$2 -v Y=$1 'NF>1{d=$1} END{print d m Y}'
1 Like

hii

can u please answer this question

if i have the week number and i know monday is the day and i know year too

how can i find out the day of the week
please as this is very imp

You could also get the yesterday date from the monday of the current week

You can also use Korn Shell 93. It has built in date arithmetic.

$ printf "%T\n" "last sunday in month"
Sun Mar 27 00:00:00 EDT 2011
$ printf "%T\n" "last week sunday"
Sun Mar  6 00:00:00 EST 2011
$ printf "%T\n" "last sunday"
Sun Mar 13 00:00:00 EST 2011
a=$(date +%w)
[[ $a -eq 0 ]] && a=7
ts=$(( $(date +%s) - $a * 24 * 3600 ))
date -d "1970-01-01 00:00:00 UTC + $ts seconds"

This is with GNU date

# date
lun mar 14 15:23:19 CET 2011
# a=$(date +%w)
# [[ $a -eq 0 ]] && a=7
# ts=$(( $(date +%s) - $a * 24 * 3600 ))
# date -d "1970-01-01 00:00:00 UTC + $ts seconds"
dim mar 13 15:23:24 CET 2011
# date
lun mar 14 15:23:33 CET 2011
#

or

# date -d "1970-01-01 00:00:00 UTC + $ts seconds" +%Y%m%d
20110313