Find the answer to your Linux question:
Results 1 to 7 of 7
Greetings, I'm new to Linux and I'm trying to detemine how to get the date that was 2 days ago. My experience is primarily with HP Tru64 UNIX and on ...
  1. #1
    Linux User
    Join Date
    Jun 2007
    Posts
    318

    How to determine date 2 days ago

    Greetings,

    I'm new to Linux and I'm trying to detemine how to get the date that was 2 days ago. My experience is primarily with HP Tru64 UNIX and on it this command works (The date being 20070605):

    # (export TZ; TZ=UTC+48; date '+%Y%m%d')
    20070603

    On RedHat Linux I get yesterday's date:
    # (export TZ; TZ=UTC+48; date '+%Y%m%d')
    20070604

    So, what's the proper way to do this on Linux.

    Thanks,
    Vic

  2. #2
    Just Joined! Sivel's Avatar
    Join Date
    Jun 2005
    Location
    Maryland
    Posts
    20
    You could do something like this:

    shell> expr $(date '+%Y%m%d') - 2

  3. #3
    Linux User
    Join Date
    Jun 2007
    Posts
    318
    Quote Originally Posted by Sivel View Post
    You could do something like this:

    shell> expr $(date '+%Y%m%d') - 2

    Thanks for the reply but that doesn't handle when the previous date is in the previous month. Say I wanted the date 5 days ago, your approach would give me:

    # expr $(date '+%Y%m%d') - 5
    20070600
    not
    20070531


    Vic

  4. #4
    Just Joined! Sivel's Avatar
    Join Date
    Jun 2005
    Location
    Maryland
    Posts
    20
    Hmmm...I overlooked that.

    Maybe perl could lend its assistance:

    Code:
    perl -le 'printf("%04d%02d%02d\n",(localtime(time-(86400*$ARGV[0])))[5] + 1900,(localtime(time-(86400*$ARGV[0])))[4],(localtime(time-(86400*$ARGV[0])))[3])' 2
    The 2 on the end could be replaced with any number. That is the number of days to subtract from the current day.

  5. #5
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    Try this:

    Code:
    date --date='2 days ago'
    Regards

  6. #6
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    you can type 'info date" and see some examples of how to use the GNU date "2 days ago" example.. (assuming you have info and GNU date of course)

  7. #7
    Linux User
    Join Date
    Jun 2007
    Posts
    318
    Quote Originally Posted by Franklin52 View Post
    Try this:

    Code:
    date --date='2 days ago'
    Regards
    Figures it would be something that simple, thanks.
    Vic

Posting Permissions

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