-
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 2012Yesterday:
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" 1329811200Confirm date/time:
bash-3.2$ date -r 1329811200 Tue Feb 21 00:00:00 PST 2012Seven 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" 1329292800Confirm date/time:
bash-3.2$ date -r 1329292800 Wed Feb 15 00:00:00 PST 2012Fourteen 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" 1328688000Confirm date/time:
bash-3.2$ date -r 1328688000 Wed Feb 8 00:00:00 PST 2012Thirtyone 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" 1327219200Confirm date/time:
bash-3.2$ date -r 1327219200 Sun Jan 22 00:00:00 PST 2012End 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 2012Yesterday @ 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" 1329897599Confirm date/time:
bash-3.2$ date -r 1329897599 Tue Feb 21 23:59:59 PST 2012