Date increment in the format "YYYYMMDD"

Hi all,

I want to increment the date which is in the format "YYYYMMDD".
ex: If the date is 20010601

Increment should be: 20010602, 20010603 etc.,

Any help will be much appreciated.

Many Thanks and Regards,
Ganapati

Hi,

Id go with a perl-onliner like this

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

note the 86400 which is the amount of seconds for a day. The advantage of this is, you dont have to care if your month has 30 or 31 days.

alternativly you could use something like this

expr `date +%Y%m%d` + 1

But that will give you a wrong date at the last day of the month.

hth
-fe

Here is on other way:

$ cat dateincr
#! /bin/ksh

[[ $# != 1 ]] && echo "Enter a numeric value pls" && exit
#Taken from Vishu's script, to validate argument
[[ `echo $1 | grep [[:alpha:]]` || `echo $1 | grep [[:punct:]]` ]] && echo "$1 is not a number" && exit 1
let hours=$1*24
date1=`TZ=CST-$hours date +%Y%m%d`
echo "Here you go $date1"
$ ./dateincr 18
Here you go 20060820

Regards,
Tayyab

hello I am getting the error for || which is used in testcondition.

I am on HPUx.

also can any more throw some light on these extended regular expressions

cheers

I'm not sure about HP-UX syntax, but you can skip validation part, only use the date code which you need in your script, this was a general date increment script, you can use something like this:

#! /bin/ksh
let hours=$1*24
date1=`TZ=CST-$hours date +%Y%m%d`
echo "Here you go $date1"

Remember this is ksh code, that matters too.

Regards,
Tayyab

1 Like