Reputation: 22610
I know what some of you are thinking: "That's easy, just use this"
date -dlast-monday +%Y%m%d
But that's not actually last Monday. That's the most recent Monday, which might be described as "this past Monday" in English.
"Last Monday" normally means the Monday of last week. Does date
have any shorthand way to get what native English speaking humans mean when they say "last Monday"?
For example, today is Tuesday when I write this. The date is 2018-09-04. I want to get 2018-08-27.
(It is at least hypothetically possible that there may be some native speakers who have a different dialect, but either way you understand my question, even if you have a different way of using the expression "last *
day".)
Upvotes: 1
Views: 2558
Reputation: 22610
I believe this may work to get the date of last Monday:
date -d"last-sunday - 6 days"
On macOS you can install the GNU utilities with Homebrew and use
gdate -d"last-sunday - 6 days"
At least in my version of GNU date last-sunday
will return the current day if it is currently Sunday. This may have changed in newer versions.
Upvotes: 1
Reputation: 531075
$ date +%F
2018-09-04
$ date -d "1 week ago last monday" +%F
2018-08-27
Of course, that sounds like it should be the week prior to last monday, or 2018-08-20. Something slightly more logical without sounding too unnatural might be
$ date -d "last monday -1 week" +%F
2018-08-27
Upvotes: 3