Languages and Scripting
cancel
Showing results for 
Search instead for 
Did you mean: 

Re: How to get a date after a specific number of days?

 
SOLVED
Go to solution
Frequent Advisor

How to get a date after a specific number of days?

Hi all,

My date is in the format mmddyy. I need to know the date after say 25 days. Is there any specific command which can accompolish this?

Thanks in Advance!!
16 REPLIES 16
Trusted Contributor

Re: How to get a date after a specific number of days?

Please find the below thread in that there is a script from A. Clay Stephenson.kindly check whether it help you or not?
http://h30499.www3.hp.com/t5/System-Administration/favourite-sysadmin-scripts-you-always-keep-around/m-p/4844855#M393717

Trusted Contributor

Re: How to get a date after a specific number of days?

Hi

could be with crontab?

Regards, Jorge
Frequent Advisor

Re: How to get a date after a specific number of days?

Hi,

Thank you all for your replies.

Is there any other specific command to do that?

Solution

Re: How to get a date after a specific number of days?

If you have the GNU coreutils installed, you can use the GNU date command, which has a pile of date arithmetic capabilities. You can get GNU coreutils here:

http://hpux.connect.org.uk/hppd/hpux/Gnu/coreutils-8.6/

Don't miss the run-time dependencies required as well.

With GNU date installed you can run:

date --d "+25 days" +%m%d%y

You'll need to put the path to the GNU date command in front of that, otherwise it will just invoke the HP-UX date command.

No doubt the perl-mongers on the forums will also be able to give you a perl one-liner for this as well.

HTH

DUncan

I am an HPE Employee
Accept or Kudo
Advisor

Re: How to get a date after a specific number of days?

Hi,

This might help you. Try this.

Result=$(perl -MPOSIX -le 'print strftime "%d%b",localtime(time+(60*60*24*25))')

Acclaimed Contributor

Re: How to get a date after a specific number of days?

Hi:

You might note that by using Perl and the 'strftime' function you can choose any date format you want. The format directives are equivalent to those you are familiar with in the 'date' command. Of course, see too the manpages for 'strftime(3C)'. For example:

# perl -MPOSIX -le 'print strftime "%d %b",localtime(time+(60*60*24*25))'
14 Mar

# perl -MPOSIX -le 'print strftime "%m/%d/%Y",localtime(time+(60*60*24*25))'
03/14/2011

# perl -MPOSIX -le 'print strftime "%m/%d/%Y %H:%M",localtime(time+(60*60*24*25))'
03/14/2011 09:08

Regards!

...JRF...





Frequent Advisor

Re: How to get a date after a specific number of days?

Hi,

That perl scripting really worked. Thanks a lot:)

But I am able to find the date after 25 days from today's date. Is there anyother modification I can do so that the perl will take my input?

Actually I need to find the date after 25 days from a specific date but not from today's date.Kindly help.
Acclaimed Contributor

Re: How to get a date after a specific number of days?

Hi:

> Actually I need to find the date after 25 days from a specific date but not from today's date.

You can begin with something like this an embellish as you like. Notice that I require the input as YYYYMMDD.

# cat ./fromwhen
#!/usr/bin/perl
use strict;
use warnings;
use POSIX qw(strftime);
use Time::Local;
my $date = shift or die "Date (YYYYMMDD) expected\n";
die "YYYMMDD expected\n" unless
my ( $yyyy, $mm, $dd ) = ( $date =~ /(\d{4})(\d\d)(\d\d)/ );
my $when = shift or die "Number of days in future expected\n";
my $secs = timelocal(0, 0, 0, $dd, $mm-1,$yyyy );
print strftime "%Y/%m/%d\n",localtime($secs+(60*60*24*$when));
1;

...run as (for example):

# .l/fromwhen 20110218 13
2011/03/03

...which yields the date thirteen (13) days from February 18, 2011.

Regards!

...JRF...
Frequent Advisor

Re: How to get a date after a specific number of days?

Hi,

Thankyou for the replies.

I tried the script. I don't how exactly to run that script. I found no good results. I think I have done some mistake. Please help me with this.