Get Monday and Sunday etc.. for a week for any date as parameter in Unix Get Monday and Sunday etc.. for a week for any date as parameter in Unix shell shell

Get Monday and Sunday etc.. for a week for any date as parameter in Unix


Try this:

export day=2013-10-01date -d "$day -$(date -d $day +%w) days"

This will always print the Sunday before the given date (or the date itself).

date -d "$day -$(date -d $day +%u) days"

This will always print the Sunday before the given date (and never the date itself).

For Mondays you need to add + 1 day:

date -d "$day -$(date -d $day +%u) days + 1 day"

You should also consider what Monday or Sunday you want to get (this wasn't quite clear) for which date. This also depends on whether you consider the Sunday the first or the last day of the week.


date can parse the date on the command line like so:

date -j -f %s 1380628152

which is the date+time in seconds since the UNIX epoch. You can combine the command abovewith your command. The -j means you don't want to actually set the date. The -f specifies a strftime string to use to parse the date on the command line.

Please note that this is on a BSD system and it looks like you are on a GNU system with different options to date (but it must support something similar).