PDA

View Full Version : Perl script to subtract days from current date


requestcode
10-23-2002, 06:24 PM
Does anybody have a Perl script that will subtract a specified number of days from the current date to come up with a new date? Thank you for any help you can give me.

requestcode
10-24-2002, 04:06 PM
Just in case anybody is interested I finally figured out how to do it. Here is what I came up with:
#!/usr/local/bin/perl
# Get number of days entered from command line.
# If the number is negative then it will give a date less
# than the current date. If positive greater than.
# This will also multiply the number of days entered times the number
# of seconds in a day (86400) to determin the number of days to add
# or subtract from the system variable time.
$myseconds=time + (@ARGV[0] * 86400);
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($myseconds);
$year=$year+1900;
$mon=$mon+1;
if($mon<10)
{$mon="0$mon";}
if($mday<10)
{$mday="0$mday";}
# Format the date in YYYYMMDD.
$mydate="$year$mon$mday";
print "Your new date is $mydate \n";


This is not my finished product, just a test to figure out how it can be done. To get a date greater that the current date you would enter the name of the script followed by the number of days. So if you call the script getdate.pl you would do this:
./getdate.pl 2

This would give you the date two days from the current date. For a date less than you would enter a negative number like this:
./getdate.pl -2

This would give you a date two days before the current date. This particular script is set up to run from the Unix prompt and display the date back. It will have to be modified to use for other purposes. Hope this helps and good luck.