Equivalent date commands (FreeBSD and GNU/Linux)

I had to put dates in a string in BASH and I had this script on Linux.
The command was something like this:

echo from=`date -d "last month" +%m/01/%Y`\&to=`date -d "$(date +%Y-%m)-01 yesterday" +%m/%d/%Y`

The above did not work on FreeBSD so I had to modify it:

echo from=`date -v-1m +%m/01/%Y`\&to=`date -v-1d -j $(date +%Y%m)010101 +%m/%d/%Y`

What the above commands do is make from = “first day of last month” and to = “last day of last month.”


More simply, to print the first day of last month in Linux:

# date -d "last month" +%m/01/%Y

and in FreeBSD

# date -v-1m +%m/01/%Y

To print the last day of last month in Linux:

# date -d "$(date +%Y-%m)-01 yesterday" +%m/%d/%Y

and in FreeBSD:

# date -v-1d -j $(date +%Y%m)010101 +%m/%d/%Y

Leave a Reply

Your email address will not be published. Required fields are marked *

To see if you are a human *