Getting Yesterday or Tomorrow with Perl

Problem

You need to get yesterday or tomorrow’s date, and you have Perl but not GNU date on your system.

Solution

Use this Perl one-liner, adjusting the number of seconds added to or subtracted from time:

# Yesterday at this same time (note subtraction)
$ perl -e "use POSIX qw(strftime); print strftime('%Y-%m-%d', localtime(time -
86400)), qq(\n);"

# Tomorrow at this same time (note addition)
$ perl -e "use POSIX qw(strftime); print strftime('%Y-%m-%d', localtime(time +
86400)), qq(\n);"

Discussion

This is really just a specific application of the recipes above, but is so common that it’s worth talking about by itself. See Figuring Out Date and Time Arithmetic for a handy table of values that may be of use.

Get bash Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.