Subscribe to my feed  Google 

Friday, December 22, 2006

Print Yesterday's Date Using Perl

Quick tip for working with Unix scripts that need yesterday's date. If you have Perl, try this:

perl -e '@y=localtime(time()-86400);printf "%04d%02d%02d",$y[5]+1900,$y[4]+1,$y[3];'

should print to stdout the date in the yyyymmdd format. Much simpler than writing an entire function in a script and messing around with 1st of the month, leap years, and possibly dates beyond 2037 (check Perl documentation).

Links:

The UNIX Forums - get yesterday in yyyymmdd format
<URL:http://www.unix.com/showthread.php?t=20181>

Technorati Tags: , , , ,

Labels:


Share
Digg This Story

Previous post:

2 Comments:

Anonymous Anonymous thought

Thanks for this! :) It works for me!

February 07, 2007 12:11 AM  
Blogger lokety thought

Good to know that. I have a few more perl scripts that I'd love to share. They help to do disk space housekeeping and performance tracking on bash and sh.

August 07, 2007 3:29 AM  

Post a Comment

Links to this post:

Create a Link