DEV Community

Xun Zhou
Xun Zhou

Posted on

How to calculate any date in MacOS bash

MacOs is not the same family of UNIX as Linux, so options for commands can be different.
By installing coreutils on MacOS, we can use the GNU/Linux date very easily as well.

brew install coreutils
Enter fullscreen mode Exit fullscreen mode

Then we can use GNU/Linux date with this command gdate instead of date on MacOS.
Here is my example by using gdate to calculate the date on MacOS.

MONDAY=$(gdate -d 'last monday' '+%d.%m.%Y')
FRIDAY=$(gdate -d 'this Friday' '+%d.%m.%Y')
Enter fullscreen mode Exit fullscreen mode

It will return the calculated date as the following(current date as I tested them: 08:05AM 31.05.2022):

echo $MONDAY
## 30.05.2022

ECHO $FRIDAY
## 03.06.2022
Enter fullscreen mode Exit fullscreen mode

Top comments (0)