If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > Removing leading zeros from dates

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-18-05, 15:13
LaMetia LaMetia is offline
Registered User
 
Join Date: Jan 2005
Posts: 2
Question Removing leading zeros from dates

Hello everyone,

What I want to do is the following:
I have a script that sets the following variable.

TODAY=`date +%m/%d/%Y`
[This will give me 01/18/2005]

I want to know how I can remove the leading zeros from the month and the day? I want to get 1/18/2005 and I want to set it to another variable so that I can pass that date to a script. Does anybody know how I can accomplish this?

Thanks!
Reply With Quote
  #2 (permalink)  
Old 01-18-05, 15:24
vgersh99 vgersh99 is offline
Registered User
 
Join Date: Apr 2004
Location: Boston, MA
Posts: 325
date +%m/%e/%Y | sed -e 's/^0//g'
__________________
vlad
+-----------------------+
| #include <disclaimer.h> |
+-----------------------+
Reply With Quote
  #3 (permalink)  
Old 01-18-05, 15:31
vgersh99 vgersh99 is offline
Registered User
 
Join Date: Apr 2004
Location: Boston, MA
Posts: 325
or [with the original "date" format]:

date '+%m/%d/%Y' | sed -e 's#^0##g;s#/0#/#g'
__________________
vlad
+-----------------------+
| #include <disclaimer.h> |
+-----------------------+
Reply With Quote
  #4 (permalink)  
Old 01-18-05, 15:40
LaMetia LaMetia is offline
Registered User
 
Join Date: Jan 2005
Posts: 2
Quote:
Originally Posted by vgersh99
or [with the original "date" format]:

date '+%m/%d/%Y' | sed -e 's#^0##g;s#/0#/#g'
Thanks vlad, so I can just do the following:

TODAY=`date '+%m/%d/%Y' | sed -e 's#^0##g;s#/0#/#g'`
?
Reply With Quote
  #5 (permalink)  
Old 01-18-05, 15:47
vgersh99 vgersh99 is offline
Registered User
 
Join Date: Apr 2004
Location: Boston, MA
Posts: 325
that's right.
__________________
vlad
+-----------------------+
| #include <disclaimer.h> |
+-----------------------+
Reply With Quote
  #6 (permalink)  
Old 01-19-05, 10:10
ddobes ddobes is offline
Registered User
 
Join Date: Jan 2005
Posts: 2
I know you allready answered this but another way of doing this is to ...

TODAY=`date +%m/%d/%Y'
X=${TODAY#0} # This will assign X to $TODAY minus the beginning characters after the #.

So if today is 01/19/2005 then ${TODAY#0} evaluates to 1/19/2005

For further clarification, if you were to use ${TODAY#01} then it would evaluate to /19/2005 instead of 01/19/2005.
Reply With Quote
  #7 (permalink)  
Old 01-19-05, 10:14
vgersh99 vgersh99 is offline
Registered User
 
Join Date: Apr 2004
Location: Boston, MA
Posts: 325
Quote:
Originally Posted by ddobes
I know you allready answered this but another way of doing this is to ...

TODAY=`date +%m/%d/%Y'
X=${TODAY#0} # This will assign X to $TODAY minus the beginning characters after the #.

So if today is 01/19/2005 then ${TODAY#0} evaluates to 1/19/2005

For further clarification, if you were to use ${TODAY#01} then it would evaluate to /19/2005 instead of 01/19/2005.
this is ksh/bash specific - I'm not sure if the OP has it.
__________________
vlad
+-----------------------+
| #include <disclaimer.h> |
+-----------------------+
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On