10

With date -d could display the future or next date

$ date -d "next mon"
Mon May 13 00:00:00 CST 2019

$ date -d "mon"
Mon May 13 00:00:00 CST 2019

Unfortunately, it failed to display the previous weekday

$ date -d "previous Monday"
date: invalid date ‘previous Monday’

How could display the previous weekday in a string-format?

14

Use last instead of previous:

$ date -d "last Monday"
Mon May  6 00:00:00 BST 2019

This is documented in info date, and the specific mention of last is in the "29.6 Day of week items" section (thanks to @Jos and @hobbs for finding it):

A number may precede a day of the week item to move forward supplementary weeks. It is best used in expression like ‘third monday’. In this context, ‘last day’ or ‘next day’ is also acceptable; they move one week before or after the day that day by itself would represent.

  • Comments are not for extended discussion; this conversation has been moved to chat. – terdon May 9 at 16:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.