The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Time Between Dates Sreejith_VK Shell Programming and Scripting 3 02-19-2008 10:06 PM
UNIX Dates ndoggy020 UNIX for Dummies Questions & Answers 9 02-14-2008 03:25 PM
Select entries between two dates by converting Unix timestamp in Oracle Database. amitsayshii UNIX for Advanced & Expert Users 1 08-08-2006 08:00 AM
How to compare two dates bankpro High Level Programming 5 01-24-2006 01:07 AM
Unix dates ssmiths001 UNIX for Dummies Questions & Answers 1 11-18-2004 07:45 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 12-03-2004
Registered User
 

Join Date: Nov 2004
Posts: 41
Need help with Unix dates

I'm very new to Unix, so forgive me for what will probably be a very quick fix. I have the following in a ksh script. Its purpose is to return yesterday's date. The $DAY part is coming back with a '2' but I need it to return as '02'. How can I do that?


date '+%b %d %Y' |
{
read MONTH DAY YEAR
DAY=`expr "$DAY" - 1`
case "$DAY" in
0)
MONTH=`expr "$MONTH" - 1`
case "$MONTH" in
0)
MONTH=12
YEAR=`expr "$YEAR" - 1`
;;
esac
DAY=`cal $MONTH $YEAR | grep . | fmt -1 | tail -1`
esac
}
Reply With Quote
Forum Sponsor
  #2  
Old 12-03-2004
zazzybob's Avatar
Registered Geek
 

Join Date: Dec 2003
Location: Melbourne, Australia
Posts: 2,100
Something like this will strip the leading zero, but will leave dates beginning with 1,2 or 3 alone...

echo `date +%d` | sed 's/[0]\([0-9]\)/\1/'

Cheers
ZB
Reply With Quote
  #3  
Old 12-03-2004
Registered User
 

Join Date: Nov 2004
Posts: 41
Where would I add that to this script?
Reply With Quote
  #4  
Old 12-03-2004
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,653
You don't want to strip a leading zero anyway. You want to add a leading zero. Try this:

((DAY < 10)) && DAY="0"$DAY

Put it after the code that sets DAY.
Reply With Quote
  #5  
Old 12-03-2004
zazzybob's Avatar
Registered Geek
 

Join Date: Dec 2003
Location: Melbourne, Australia
Posts: 2,100
Re-reading your OP, you want to ADD the leading zero on dates less than 10, correct?

If so add
Code:
DAY=`echo $DAY | sed 's/^\(.\)$/0\1/'`
Cheers
ZB
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 02:58 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0