Results 1 to 7 of 7
  1. #1
    Join Date
    Jan 2005
    Posts
    2

    Question Unanswered: 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!

  2. #2
    Join Date
    Apr 2004
    Location
    Boston, MA
    Posts
    325
    date +%m/%e/%Y | sed -e 's/^0//g'
    vlad
    +-----------------------+
    | #include <disclaimer.h> |
    +-----------------------+

  3. #3
    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> |
    +-----------------------+

  4. #4
    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'`
    ?

  5. #5
    Join Date
    Apr 2004
    Location
    Boston, MA
    Posts
    325
    that's right.
    vlad
    +-----------------------+
    | #include <disclaimer.h> |
    +-----------------------+

  6. #6
    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.

  7. #7
    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> |
    +-----------------------+

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •