1. G. Bash: past dates in epoch format

    Here are some notes on converting dates to epoch and tweaking what dates are displayed. For the purpose of this, I’m trying to pass start and end dates in epoch format to a REST API. I’m looking for ranges which include: yesterday, last 7 days, last 14 days and last 31 days.

    Start Time

    Note: I set this up so that by changing the DAYS_AGO, you can control how far back in time you go.

    Current Date/Time:

        bash-3.2$ date
        Wed Feb 22 13:32:13 PST 2012
    

    Yesterday:

        bash-3.2$ DAYS_AGO="1"; date -j -f "%a %b %d"  -j -f "%a %b %d %T %Z %Y" "`date -v-${DAYS_AGO}d -v00H -v00M -v00S`" "+%s"
        1329811200
    

    Confirm date/time:

        bash-3.2$ date -r 1329811200
        Tue Feb 21 00:00:00 PST 2012
    

    Seven days ago:

        bash-3.2$ DAYS_AGO="7"; date -j -f "%a %b %d"  -j -f "%a %b %d %T %Z %Y" "`date -v-${DAYS_AGO}d -v00H -v00M -v00S`" "+%s"
        1329292800
    

    Confirm date/time:

        bash-3.2$ date -r 1329292800
        Wed Feb 15 00:00:00 PST 2012
    

    Fourteen days ago:

        bash-3.2$ DAYS_AGO="14"; date -j -f "%a %b %d"  -j -f "%a %b %d %T %Z %Y" "`date -v-${DAYS_AGO}d -v00H -v00M -v00S`" "+%s"
        1328688000
    

    Confirm date/time:

        bash-3.2$ date -r 1328688000
        Wed Feb  8 00:00:00 PST 2012
    

    Thirtyone days ago:

        bash-3.2$ DAYS_AGO="31"; date -j -f "%a %b %d"  -j -f "%a %b %d %T %Z %Y" "`date -v-${DAYS_AGO}d -v00H -v00M -v00S`" "+%s"
        1327219200
    

    Confirm date/time:

        bash-3.2$ date -r 1327219200
        Sun Jan 22 00:00:00 PST 2012
    

    End Time

    Note: This works for all examples, at we always want to get from yesterday at 23:59:59.

    Current Date/Time:

        bash-3.2$ date
        Wed Feb 22 13:35:11 PST 2012
    

    Yesterday @ 23:59:59:

        bash-3.2$ date -j -f "%a %b %d"  -j -f "%a %b %d %T %Z %Y" "`date -v-1d -v23H -v59M -v59S`" "+%s"
        1329897599
    

    Confirm date/time:

        bash-3.2$ date -r 1329897599
        Tue Feb 21 23:59:59 PST 2012
    

    3 months ago  /  0 notes