subtract 1 hour from date in unix shell script

前端 未结 8 1062
鱼传尺愫
鱼传尺愫 2020-12-16 08:42

I have the following in a shell script. How can I subtract one hour while retaining the formatting?

DATE=`date \"+%m/%d/%Y -%H:%M:%S\"`
相关标签:
8条回答
  • 2020-12-16 09:31

    Convert to timestamp (a long integer), subtract the right number of milliseconds, reformat to the format you need.

    Hard to give more details since you don't specify a programming language...

    0 讨论(0)
  • 2020-12-16 09:34

    The following command works on recent versions of GNU date:

    date -d '1 hour ago' "+%m/%d/%Y -%H:%M:%S"
    
    0 讨论(0)
  • 2020-12-16 09:34

    Here another way to subtract 1 hour.

    yesterdayDate=`date -d '2018-11-24 00:09 -1 hour' +'%Y-%m-%d %H:%M'` 
    echo $yesterdayDate
    
    Output:
    2018-11-23 23:09
    

    I hope that It can help someone. Best Regards!

    0 讨论(0)
  • 2020-12-16 09:38
    $ date +%Y-%m-%d-%H 
    2019-04-09-20
    
    $ date -v-1H +%Y-%m-%d-%H 
    2019-04-09-19
    

    But in shell use as like date +%Y-%m-%d-%H, date -v-1H +%Y-%m-%d-%H

    0 讨论(0)
  • 2020-12-16 09:39

    if you need substract with timestamp :

    timestamp=$(date +%s -d '1 hour ago');
    
    0 讨论(0)
  • 2020-12-16 09:40

    This work on my Ubuntu 16.04 date: date --date="@$(($(date +%s) - 3600))" "+%m/%d/%Y -%H:%M:%S" And the date version is date (GNU coreutils) 8.25

    0 讨论(0)
提交回复
热议问题