#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2011
    Posts
    6
    Rep Power
    0

    Help Requested - how to find last Sunday's date if a date is given


    Hi All,

    I have got a requirement for my project. It is as follows,

    If I pass today's date as a parameter to a command/script, it should derive the previous Sunday's date. ( ie if 201200307 is passed, the command/script should return 20120304).

    Is there a way we can do it ? I would prefer to use a command instead of a script. Any thoughts will be appreciated.

    Thanks
    Freddie
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2011
    Posts
    6
    Rep Power
    0
    Thanks for taking a glance of the issue.

    I have found a solution for this as below,

    export DAYS_AGO=`date +%w`
    echo $DAYS_AGO
    export SUB_DATE=`date -d "-$DAYS_AGO day" "+%Y%m%d"`
    echo "P_SUB_DATE=$SUB_DATE" >>$COMMON_TMP/CPMGDynamicDateFile.txt

    I am not sure whether this command will work fine for all the dates of a month (like for e.g: 20120302). Will it give the correct date as expected.

    Any thoughts will be much appreciated.

    Thanks
    Freddie
  4. #3
  5. No Profile Picture
    Grumpier old Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jun 2003
    Posts
    14,443
    Rep Power
    4539
    Thanks for posting your solution.
    ======
    Doug G
    ======
    Bartender to Rene Descartes "have another beer?" Descartes: "I think not" and he vanished.
    --Alfred Bester
  6. #4
  7. D'oh!
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Location
    East Coast
    Posts
    75
    Rep Power
    73
    The date command is *way* more powerful than having to go through all of that though:
    Code:
    date -dlast-sunday +%Y%m%d

IMN logo majestic logo threadwatch logo seochat tools logo