Get business days including today's date

I am trying to get last 5 business day [excluding sat and sun].
trying

for d in Mon Tue Wed Thu Fri
do
date +%Y%m%d -d "last $d"
done

gives me [provided]

date
Thu Oct 20 23:56:26 EDT 2016
20161017
20161018
20161019
20161013
20161014

expected output should be

20161017
20161018
20161019
20161020
20161014

Not trying to interpret your specification but just guessing what you want (for date: 21.10.16 09:57 ) :

{ date +%Y%m%d; for d in Mon Tue Wed Thu Fri; do date +%Y%m%d -d "last $d"; done; } | sort | tail -5
20161017
20161018
20161019
20161020
20161021
1 Like

You haven't said what operating system or shell you're using (and the date utility on my operating system doesn't support the -d option), but with a recent Korn shell, you could use:

#!/bin/ksh
dow=$(date '+%a')
for d in Mon Tue Wed Thu Fri
do
	[ $d = $dow ] && offset="now" || offset="last $d"
	#date +%Y%m%d -d "$offset"
	printf '%(%Y%m%d\n)T' "$offset"
done

If you don't have a recent Korn shell and do have a date utility that has a -d option, you could comment out the printf command in that loop and uncomment the date command.

1 Like