3

How to use the date command, and given a timestamp, to get the timestamp X days ago. For example UTC time stamp is 1525192000, I want to get the UTC timestamp 30 days before that.

0

1 Answer 1

6

For a start, your time stamp is in seconds since 1 January 1970 00:00:00 UTC is my assumption, if it is not, convert it to that.

So, you have to subtract the seconds for the days you want.

stamp=1525192000
day=$((60*60*24))
echo $(($stamp - 30*$day))

You then get a Unix epoch time back, if that needs conversion to date and time of day, you may have to take DST into account, and that's where you may need to use the date utility.

Note, that you can also use date. This allows you to work with other date elements easily, no calculation needed:

With GNU date:

date --date="$(date --iso-8601=s -d @1525192000) -30 days" +%s

Example:

 $ date --date="$(date --iso-8601=s -d @1525192000)"
 Tue, May  1, 2018  7:06:01 PM
 $ date --date="$(date --iso-8601=s -d @1525192000) -30 days"
 Wed, Apr 1, 2018  7:06:01 PM
 $ date --date="$(date --iso-8601=s -d @1525192000) -30 days" +%s
 1523466380
 $ date --date="$(date --iso-8601=s -d @1525192000) -30 days -1 week -3 months" +%s
 1514309200

In FreeBSD and macOS (the -j means do not attempt to set system date, nice and safe)

date -j -f "%a %b %d %T %Z %Y" "$(date -r 1234567890)"  -v-30d "+%s"

EDIT: Thanks, Kusalananda, OpenBSD and NetBSD do not support -v.

10
  • @StephenKitt, 24 hours of 3600 seconds each is one of the most common definition of day. It's true that when considering calendar dates, there are other definition of day some of which can have 23 or 25 hours because of DST change, one might want to consider leap seconds in some contexts, but here the OP is not asking for dates and is not mentioning any calendar representation, just what looks like a Unix epoch timestamp. Taking 30x24x60x60 off that number is the only reasonable answer to give here without more context. May 11, 2018 at 8:13
  • 2
    Note that I was referring to the downvote on your answer (which currently has one down vote and one up vote (mine, before the edit)), not the question. Note that the OP is not making any reference to any date (other than the date utility) or any calendar. For all we know, they may not be using the Gregorian calendar. May 11, 2018 at 8:54
  • 1
    Note that Unix epoch time is not related to timezones and not much to UTC. They identify instant in times based on an offset to another precise instant in time (1970-01-01 00:00:00 UTC even if UTC didn't exist at the time). That's as a number of Unix seconds that are not UTC seconds. They (contrary to UTC seconds) have a variable duration as there are always 86400 of them in an Earth solar day. So substracting 30x24x60x60 is again the correct thing to do unless you want to define "day" as 86400 UTC seconds (which doesn't match the earth solar day) May 11, 2018 at 9:08
  • 1
    Your "BSD" does not include OpenBSD.
    – Kusalananda
    May 11, 2018 at 9:28
  • 1
    Hope you don't mind the edits which fix some of the issues in your answer. May 11, 2018 at 10:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .