Get Monday and Sunday etc.. for a week for any date as parameter in Unix
14,189
Try this:
export day=2013-10-01
date -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.
Related videos on Youtube
Comments
-
Mayukh Roy almost 2 years
How to get the date of Monday and Sunday in a week for a date?
This gives date for 'last' monday:
date -dlast-monday +%Y%m%d
I want to pass a date as parameter to find the Monday and Sunday for that week. Basically, I want to get Sunday and Monday for a week, for ANY date, NOT only for last monday.
-
Naveenan over 6 yearshow do I format the output of this in YYYY-MM-DD format ?
-
Alfe over 6 yearsAppend
+%F
to the command line:date -d "$day -$(date -d $day +%u) days" +%F