0

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".)

divibisan
  • 11,659
  • 11
  • 40
  • 58
iconoclast
  • 21,213
  • 15
  • 102
  • 138

2 Answers2

2
$ 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
chepner
  • 497,756
  • 71
  • 530
  • 681
  • If I run this on Sunday, will I get the Monday that is 6 days prior? It doesn't look like it... – iconoclast Sep 04 '18 at 17:09
  • @iconoclast Do native English speakers mean the Monday 6 days prior when they say "last Monday" on Sunday? I.e., does this assume Sunday as the first day of the next week? – Benjamin W. Sep 04 '18 at 17:19
  • At what point does LAST MONDAY increment? Thursday? Friday? – Jack Sep 04 '18 at 17:21
  • @BenjaminW.: interesting questions, but they belong in the English Stack Exchange, not here. I'll grant that in fact it's more complex than _the `*`day of last week_, but I think that for purposes of this question my simple analysis is good enough. And I don't think I've ever found anyone who would refer to _yesterday_ as "last `*`day". – iconoclast Sep 04 '18 at 19:38
  • @Jack: are you asking about English, or about the desired behavior for a `date` command? If the former, I think my comment above applies. If the later, it might actually be nice to be able to specify that in the command but I doubt that that's possible. – iconoclast Sep 04 '18 at 19:41
  • @iconoclast Well, I'm used to Monday being the first day of the week, so if "last Monday" is "Monday of last week" and I say it on Sunday, I expect the Monday 13 days ago to be the answer (which chepner's answer provides), but you seem to start the new week on Sunday and expect the Monday 6 days ago to be the answer. – Benjamin W. Sep 04 '18 at 19:48
  • @iconoclast Apparently, in GNU date, "last" is equivalent to "-1": https://www.gnu.org/software/coreutils/manual/coreutils.html#General-date-syntax; for day of week items (https://www.gnu.org/software/coreutils/manual/coreutils.html#Day-of-week-items), "‘last day’ or ‘next day’ is also acceptable; they move one week before or after the day that day by itself would represent." – Benjamin W. Sep 04 '18 at 19:52
  • "last X" in English is always relative to the current date, regardless of which day you might consider the week to begin on. The ambiguity is to whether it refers to the most recent X, or the X prior to the most recent X. – chepner Sep 04 '18 at 20:06
  • @iconoclast The desired behavior for your program. And yes, it is possible. "Anything's possible in an animated cartoon" -Bugs Bunny – Jack Sep 04 '18 at 21:44
1

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.

iconoclast
  • 21,213
  • 15
  • 102
  • 138
  • I have GNU date v. 8.27. It does not return the current day. – Jack Sep 04 '18 at 17:22
  • @Jack: I'll try updating and see if I can get the behavior you describe, and try to update the answer.... although the old/buggy behavior of `date` seems like it's actually preferable for my current purposes. – iconoclast Sep 04 '18 at 19:34