-   Programming (
-   -   Date command - month of year, blank padded? (

menator 06-27-2006 05:52 AM

Date command - month of year, blank padded?
Im trying to create a variable in my shell script that contains todays date but with the day and month padded with blanks:


date +%e/%m/%Y
This would return 27/06/2006. The day of the month is padded with blanks so that you get '1' instead of '01' in the day (e.g. 1/06/2006 instead of 01/06/2006). The problem is that there doesnt seem to be a way to do the same for the month of the year...well not according to the man page anyway.

Is there I way i can achieve this?

kassle 06-27-2006 06:12 AM

ok, so you want to show 1/6/2006. try the following script

month=`date +%m`
zero=`echo $month | cut -c 1`
if [ $zero -le 0 ]; then
month=`echo $month | cut -c 2`
echo `date +%e`"/$month/"`date +%Y`

menator 06-27-2006 06:36 AM

oh that worked perfectly - thanks for the help :)

jtshaw 06-27-2006 07:00 AM

You have other options too...


date +%_d/%m/%y
From the man page on my machine:

%e    day of month, space padded; same as %_d
<EDIT> ok.. I can read...


date +%_d/%_m/%y
You can put the _ in any construct to get space padding...


j_shaw@jshawlptp:~$ date +%_d/%_m/%y
27/ 6/06

All times are GMT -5. The time now is 09:24 PM.