current date - 8 months in perl script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting current date - 8 months in perl script
# 1  
Old 08-30-2012
current date - 8 months in perl script

I have a requirement as follows.

when i pass a date to the perl script, it has to calculate the current date - 8 months and output the date back to the shell script in date format (YYYY-MM-DD).

Current date - 8 months is not constant.. because leap year, and the months jan, mar, may,.... has 31 days and april, june,.... has 30 days.

The output should be accurate.

Can anyone provide a solution for this issue.

Thanks
Krishnakanth
# 2  
Old 08-30-2012
Code:
use Date::Calc qw(Add_Delta_YM);

my $date_input = '2000-08-01';

my @date = split /-/ ,$date_input;

($year,$month,$day) =  Add_Delta_YM($date[0],$date[1],$date[2],0,-8);

if ($month < 10) {
	$month = 0 . $month;
}
return $year . '-' . $month . '-' . $day;

try that

Last edited by Franklin52; 08-31-2012 at 06:26 AM.. Reason: Please use code tags for data and code samples
# 3  
Old 08-30-2012
Try:
Code:
#!/usr/bin/perl
use POSIX;
my @t = localtime(time);
print strftime("%Y-%m-%d\n", localtime(mktime(0,0,0,$t[3],$t[4]-8,$t[5],0,0)));

---------- Post updated at 11:58 AM ---------- Previous update was at 11:19 AM ----------

Or if you want to read date from stdin, validate and then output 8 months earlier:

8month.pl:
Code:
#!/usr/bin/perl
use Time::Local 'timelocal';
use POSIX;
my $year, $month, $day;
while (my $ln = <STDIN>) {
  if($ln =~ /(\d\d\d\d).(\d\d).(\d\d)/) {
    $ln =~ s/(\d\d\d\d).(\d\d).(\d\d)/\1 \2 \3/;
    ($year, $month, $day) = split(/\s/, $ln);
    break unless timelocal(0,0,0,$day,$month-1,$year);
  }
}
print strftime("%Y-%m-%d\n", localtime(mktime(0,0,0,$day,$month-9,$year-1900,0,0))) unless ($day == 0);

Examples:
Code:
$ echo "2012-08-31" | ./8month.pl
2011-12-31
 
$ echo "2012-18-31" | ./8month.pl
Month '17' out of range 0..11 at ./8month.pl line 9
 
$ echo "2012/01/01" | ./8month.pl
2011-05-01
 
$ echo "2012-10-30" | ./8month.pl
2012-03-01

# 4  
Old 08-31-2012
If perl is not mandatory and if you have GNU date, you could do this:
Code:
date -d "-8 months" +%Y-%m-%d

# 5  
Old 08-31-2012
Using dateutils this would be:

dadd today -8mo
=>
2011-12-31
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Linux

How to calculate the quarter end date according to the current date in shell script?

Hi, My question is how to calculate the quarter end date according to the current date in shell script? (2 Replies)
Discussion started by: Divya_1234
2 Replies

2. UNIX for Beginners Questions & Answers

UNIX script to replace old date with current date dynamically in multiple files present in a folder

I am trying to work on a script where it is a *(star) delimited file has a multiple lines starts with RTG and 3rd column=TD8 I want to substring the date part and I want to replace with currentdate minus 15 days. Here is an example. iam using AIX server $ cat temp.txt RTG*888*TD8*20180201~... (1 Reply)
Discussion started by: Shankar455
1 Replies

3. Shell Programming and Scripting

Comparing the dates with the current date in perl scripting

Hi i have a file containg dates likebelow 4/30/2013 3/31/2013 4/30/2013 4/16/2013 4/30/2013 4/30/2013 5/30/2013 5/30/2013 4/30/2013 5/30/2013 5/30/2013 3/31/2013 now i want to compare the above dates with current date and i want to display the difference . (10 Replies)
Discussion started by: siva kumar
10 Replies

4. Shell Programming and Scripting

Fetch date of 7 years back from current date in Perl

$beginDate = substr(DateCalc("today", "-7Days"),0,8); This fetches the date 7 days back Can I fetch the date before 7 years from todays date in Perl using same syntax Use code tags, see PM. (3 Replies)
Discussion started by: parthmittal2007
3 Replies

5. Shell Programming and Scripting

how to append current date to filename.tgz in perl

i would like to know how to append current date in a filename with .tgz extension. #!/usr/bin/perl my $date = `date + %Y%m%d`; system("sudo mv /tmp/nyucs01_config_backup.tgz /misc/nyucs01_config_backup_$date.tgz"); im getting this error message: sh: line 1: .tgz: command not found (7 Replies)
Discussion started by: linuxgeek
7 Replies

6. Shell Programming and Scripting

Substracting days from current date(K shell script)

Hi, I want to subtract 'n' days from the current timestamp in a k shell script. Is there any inbuilt function to do it or any workaround solution to get the date. And I want the output to be in YYYY:MM:DD HH:MM:SS format. Please help. Thanks in advance. (4 Replies)
Discussion started by: Suryaaravindh
4 Replies

7. UNIX and Linux Applications

sqlite: calculating with dates - compare current date minus 6 months with stored record

Hi I have a table with name, date in format DD.MM.YYYY. I need to something like this (I try to explain in pseudo code) if SYSDATE (current date) minus 6 months > $expiry date print OK else print NOK with $name and $expiry date I know this is possible with Oracle. How to do this... (0 Replies)
Discussion started by: slashdotweenie
0 Replies

8. Shell Programming and Scripting

How to Get 60 days Old date from current date in KSH script

Hi i am writing a cron job. so for it i need the 60 days old date form current date in variable. Like today date is 27 jan 2011 then output value will be stote in variable in formet Nov 27. i am using EST date, and tried lot of solution and see lot of post but it did not helpful for me. so... (3 Replies)
Discussion started by: Himanshu_soni
3 Replies

9. Shell Programming and Scripting

Perl: Extracting date from file name and comparing with current date

I need to extract the date part from the file name (20080221 in this ex) and compare it with the current date and delete it, if it is a past date. $file = exp_ABCD4_T-2584780_upto_20080221.dmp.Z really appreciate any help. thanks mkneni (4 Replies)
Discussion started by: MKNENI
4 Replies

10. Shell Programming and Scripting

Perl: Run perl script in the current process

I have a question regarding running perl in the current process. I shall demonstrate with an example. Look at this. sh-2.05b$ pwd /tmp sh-2.05b$ cat test.sh #! /bin/sh cd /etc sh-2.05b$ ./test.sh sh-2.05b$ pwd /tmp sh-2.05b$ . ./test.sh sh-2.05b$ pwd /etc sh-2.05b$ So... (10 Replies)
Discussion started by: vino
10 Replies
Login or Register to Ask a Question