Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

date manipulation question

Status
Not open for further replies.

srsr

Programmer
May 15, 2006
3
CA
i am manipulating the date in a unix script as follows:

if [ $Day != 1 ]
then
Month=$(($Month + 1)) && Day=1

if [ $Month = 12 ]
then
Month=1 && Year=$(($Year + 1))
fi
fi


however i want to prefix the result to a filename, however it does not include zeros
eg 20020301 (1st march 2002) would be 200231

can anyone suggest a way to include the 0's
 
Hi,

you can use the printf command to format the date :

Prefix=$(printf "%04d%02d%02d" $Year $Month $Day)
Jean Pierre.
 
hi aigles

i am not sure what you mean by the above example?

if i have worked out that teh month is 02 and i want to add
1 to it, this cahnges the month to 3 without the leading zero. i want to keep the leading zeros? i can do this using printf??

thanks
srsr
 
use printf after manipulating the date :


if [ $Day != 1 ]
then
Month=$(($Month + 1)) && Day=1

if [ $Month = 12 ]
then
Month=1 && Year=$(($Year + 1))
fi
fi

Prefix=$(printf "%04d%02d%02d" $Year $Month $Day)


In your example, for the date 20020211 Prefix will be : 20020301

Jean Pierre.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top