Obtain epoch time and calculate date yesterday


You want to capture the current epoch. Maybe to use in a log file, or as a filename.

Or maybe you want to calculate the date yesterday.



This piece of code is very useful for performing date calculations. You can obtain the current epoch (time in seconds since Jan 1 1970), then add 3600 for 1 hour – or 86400 for 24 hours hence.



So use in a UNIX variable like this:

epoch=perl -M'English' -e 'print $BASETIME."n";'

To work out 24 hours ago, just subtract 86400.

perl -M'English' -e 'print(($BASETIME-86400)."n");'

Then to see the date yesterday:

$ perl -M'English' -e 'print(($BASETIME-86400)."n");'
$ perl -M'English' -e 'print(localtime(1180746252)."n");'
Sat Jun 2 09:04:12 2007



[tags]Perl, epoch, date manipulation, Perl Coding School[/tags]




If you have found my website useful, please consider buying me a coffee below 😉

Leave a Reply

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