LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Date command - month of year, blank padded? (https://www.linuxquestions.org/questions/programming-9/date-command-month-of-year-blank-padded-458687/)

menator 06-27-2006 04: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:


Code:

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 05:12 AM

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

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

menator 06-27-2006 05:36 AM

oh that worked perfectly - thanks for the help :)

jtshaw 06-27-2006 06:00 AM

You have other options too...

Code:

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

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

try:
Code:

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

Code:

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



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