Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 06-27-2006, 04:52 AM   #1
LQ Newbie
Registered: Nov 2005
Posts: 11

Rep: Reputation: 0
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?
Old 06-27-2006, 05:12 AM   #2
Registered: Jan 2006
Distribution: bodhi
Posts: 44

Rep: Reputation: 15
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`
Old 06-27-2006, 05:36 AM   #3
LQ Newbie
Registered: Nov 2005
Posts: 11

Original Poster
Rep: Reputation: 0
oh that worked perfectly - thanks for the help
Old 06-27-2006, 06:00 AM   #4
Senior Member
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
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

Last edited by jtshaw; 06-27-2006 at 06:03 AM.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
transform month number to month name in php ALInux Programming 1 11-09-2005 10:45 AM
Multicount in PHP , displays visits on current day, week, month, year and total visit xbaez Programming 1 04-24-2005 02:50 AM
How do I print using format that padded space between printed value ? Linh Programming 2 06-18-2004 03:22 PM
Starting day of month, month length chrisk5527 Programming 2 03-03-2004 04:03 PM
How to get the month in one digit instead of two digits in the "date" command? aadaileh Programming 7 02-05-2004 04:57 PM > Forums > Non-*NIX Forums > Programming

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

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration