The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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-20-2008 01:06 AM
UNIX Dates ndoggy020 UNIX for Dummies Questions & Answers 9 02-14-2008 06:25 PM
Select entries between two dates by converting Unix timestamp in Oracle Database. amitsayshii UNIX for Advanced & Expert Users 1 08-08-2006 11:00 AM
How to compare two dates bankpro High Level Programming 5 01-24-2006 04:07 AM
Unix dates ssmiths001 UNIX for Dummies Questions & Answers 1 11-18-2004 10:45 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 12-03-2004
ssmiths001 ssmiths001 is offline
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
}
  #2 (permalink)  
Old 12-03-2004
zazzybob's Avatar
zazzybob zazzybob is offline Forum Advisor  
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
  #3 (permalink)  
Old 12-03-2004
ssmiths001 ssmiths001 is offline
Registered User
  
 

Join Date: Nov 2004
Posts: 41
Where would I add that to this script?
  #4 (permalink)  
Old 12-03-2004
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,111
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.
  #5 (permalink)  
Old 12-03-2004
zazzybob's Avatar
zazzybob zazzybob is offline Forum Advisor  
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
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 02:46 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0