date

Removing Leading Zero in Date Format

The date command allows us to display the current date and time.

$ date
Sun May 26 01:16:25 +08 2019

Most importantly, we can format the date and time using specifications from strftime. Now, assume we want a format like 26/5.

$ date '+%d/%m'
26/05

Notice that %m yielded 05 instead of 5. This is because the values for %m are 01 - 30. So, to remove the leading zero, we need to do %-m. The same goes for %-d.

$ date '+%-d/%-m'
26/5