I want to get a date in the format 'YYYYMMdd' (for example, today would be 20110627) format for the monday of the current week. From tomorrow through to Sunday I'd like to still print out Mondays (today's) date. Then Next week, repeat the process
-
Thinking you'd have to do some math using Week Number of the year and $(date) formatting... – Tom Ritter Jun 27 '11 at 19:03
-
rereading this, are you asking for a range like monday-nextsunday? – matchew Jun 27 '11 at 19:07
-
1Some unixen allow `--iso-8601`, which gives you "%Y-%m-%d", which I find easier to scan manually for dates. – Matthew Schinckel Dec 02 '11 at 05:06
-
Yep, basically `date +"%F"` does this nice (adding to Metthew Schinckel's comment). So, applying this to the question, if you have current date as `day=$(date +"%F")` then `date -d "$day -$(date -d $day +%u) days + 1 day" +"%F"` should always give you the Monday date (or replace `%F` with `%Y%m%d` to have the format requested). – RAM237 Feb 21 '17 at 16:19
5 Answers
#monday
date -dmonday +%Y%m%d
#last monday
date -dlast-monday +%Y%m%d
#next monday
date -dnext-monday +%Y%m%d
#two mondays from now
date -d'monday+14 days' +%Y%m%d
#two mondays ago
date -d'monday-14 days' +%Y%m%d
#although, if you fancy yourself an Abraham Lincolin
date -d'monday-fortnight ago' +%Y%m%d #2 weeks ago
date -d'monday+fortnight' +%Y%m%d #2 weeks from now
#Monday Next Year
date -d'52+monday' +%Y%m%d
#However, Monday Last Year
date -d'52-monday' +%Y%m%d #DOES NOT WORK
#you can try a day other than monday
#and format this differently.
if a range is what your after you may need to do a few things
#Tuesday to Sunday
#since today is monday, I'll use Tuesday
echo `date -dtuesday +%Y%m%d-``date -dnext-sunday +%Y%m%d`
which would output:
20110628-20110703
note this only works on GNU date
I have read that:
Solaris version of date, which unable to support
-d
can be resolve with replacing sunfreeware.com version of date

- 19,195
- 5
- 44
- 48
-
-
so it does...good catch. I probably only tried one at a time. So much for my attempt to be witty. the correct solution would be `date -d'monday-fortnight ago' +%Y%m%d` – matchew Dec 02 '11 at 05:47
-
6None of these is giving exactly what the original question asked, "monday" is the date of the nearest monday _in the future_ (if today is not monday), so that would not give the monday of the current week. This week's monday can be given by the slightly roundabout `date -d"next-monday - 1week"` – vivi Dec 06 '17 at 16:15
-
1Yeah, I've found that this solution doesn't work but went with `date -d"last-sunday + 1 day"` instead. – Panda TG Attwood Feb 06 '18 at 16:37
-
-
@PandaTGAttwood For a Monday this might work, but adapting this for a Sunday might fail. – kvantour Oct 08 '20 at 08:05
-
Great answer! How to have the monday date, starting from year (in example 2021) and a week number (in example 22)? – aborruso Feb 02 '22 at 09:30
For those of us without GNU dates (like us OS Xers), we may have the "-v" parameter
You can then do this:
# Most recent Monday
date -v -Mon
# Output as of this writing
Mon Jun 24 12:35:48 EDT 2013
date -v -Mon "+%Y%m%d"
# Outputs
20130624
This also seems to not be a problem if today is Monday, in my current case Thursday
# Today's date
date
# Outputs
Thu Jun 27 12:41:39 EDT 2013
# Most recent Thursday
date -v -Thu
# Outputs
Thu Jun 27 12:41:46 EDT 2013

- 484
- 4
- 13
-
Frustrating, that GNU date doesn't have -v, so this solution also can't be used only both Mac and Linux. – Lloyd Dewolf Apr 25 '15 at 23:58
-
3On Max, GNU date is available from homebrew with `brew install coreutils`. After that, you can use it as 'gdate'. – Ben XO Feb 26 '19 at 11:15
-
If you used some other logic to pass in the number of days you could use +/- days also, that might help though make more complex `date -v'+2d' "+%Y-%m-%d"` Or last Tues to next Sunday `echo `date -v -Tue "+%Y-%m-%d - "``date -v +Sun "+%Y-%m-%d"`` – tristanbailey Oct 26 '20 at 14:36
Try this to get the current Monday's date.
wd=`date +%u`;
let wd=wd-1;
mon=`date --date="-$wd day" +%Y%m%d`;

- 64,273
- 8
- 118
- 148
-
5as a one-liner: `mon=$(date -d "$(( $(date +%u) - 1 )) days ago" +%Y%m%d)` – glenn jackman Jun 27 '11 at 20:03
-
1I like. Never tried baking it that way: always thought lightning would strike me down for trying to compress all that into one line :) – Femi Jun 27 '11 at 20:13
If you want the monday previous to a given date, like a start of week function, there's a little more to it. That's what I was looking for when I searched up this question, so I'll add my answer here.
I tried to take a "reusable tool" approach.
My monday
script:
#!/bin/bash
date=${1:-now}
shift
dow=`date +%u -d "$date"`
dow0=$[ $dow - 1 ]
date -d "$date -$dow0 days" "$@"
How it works:
- get
date -d
argument from first command line argument, e.g. "2020-01-01 -2 weeks" - additional args are passed on to the final date command, e.g. to format with
+%F
- get the day of week with %u (1..7); 1 is Monday
- subtract 1 as we want the range 0 to 6.
- show the date for the given date, minus the "day of the week" offset
A similar sunday
script, which turns out to be a little simpler:
#!/bin/bash
date=${1:-now}
shift
dow=`date +%w -d "$date"`
date -d "$date -$dow days" "$@"
Usage:
# monday
Mon Aug 29 02:28:45 PM AEST 2022
# sunday now +%F
2022-08-28
# monday '2022-12-01' +%F
2022-11-28

- 7,819
- 3
- 38
- 38