Bash script/command to print out date 5 min before/after

后端 未结 4 810
刺人心
刺人心 2021-01-01 16:03

I need to somehow use the date command in bash or another utility to print out the date and time, 5 minutes before and 5 minutes after a given value. For example:

i

4条回答
  •  一生所求
    2021-01-01 17:01

    With GNU date, you can do a simple form of date/time arithmetic with the argument to the --date option:

    $ date --date 'now + 5 minutes'
    

    With BSD date (at least, the version that ships with Mac OS X), the -v option allows you to do something similar, but only with the current time:

    $ date -v +5M
    $ date -v -5M
    

    Otherwise, you need to perform the math explicitly after converting the time to a UNIX timestamp (seconds since Jan 1, 1970):

    $ date -r $(( $(date +%s) + 300 ))   # 5 minutes (300 seconds) from now
    $ date -r $(( $(date +%s) - 300 ))   # 5 minutes ago
    

提交回复
热议问题