68

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"`
Mateusz Piotrowski
  • 8,029
  • 10
  • 53
  • 79
shamir chaikin
  • 681
  • 1
  • 6
  • 9

9 Answers9

99

The following command works on recent versions of GNU date:

date -d '1 hour ago' "+%m/%d/%Y -%H:%M:%S"
dogbane
  • 266,786
  • 75
  • 396
  • 414
  • 1
    i recieve -date: illegal option -- d usage: date [-u] mmddHHMM[[cc]yy][.SS] date [-u] [+format] date -a [-]sss[.fff] – shamir chaikin May 09 '11 at 08:40
  • @shamir this means your version of date does not support this option. – dogbane May 09 '11 at 08:41
  • i figure it out - i post it because may be you know some other way to do it on solaris machine? – shamir chaikin May 09 '11 at 09:16
  • 1
    @shamir, it may be easiest to just install GNU date. You can use a different prefix or path so it doesn't conflict with the "official" one. When I used to do a lot of work on Solaris, I installed a number of GNU utilities in my /home/karl/bin because they have these handy extra options. – Karl Bielefeldt May 09 '11 at 23:48
19
date -v-60M "+%m/%d/%Y -%H:%M:%S"

DATE=`date -v-60M "+%m/%d/%Y -%H:%M:%S"`

If you have bash version 4.4+ you can use bash's internal date printing and arithmetics:

printf "current date: %(%m/%d/%Y -%H:%M:%S)T\n"
printf "date - 60min: %(%m/%d/%Y -%H:%M:%S)T\n" $(( $(printf "%(%s)T") - 60 * 60 ))

The $(printf "%(%s)T") prints the epoch seconds, the $(( epoch - 60*60 )) is bash-aritmetics - subtracting 1hour in seconds. Prints:

current date: 04/20/2017 -18:14:31
date - 60min: 04/20/2017 -17:14:31
clt60
  • 62,119
  • 17
  • 107
  • 194
  • 1
    This is not portable, and relies on BSD `date` features. – tripleee Nov 26 '20 at 08:37
  • @tripleee and which date is "portable"? Remember here are zillion BSD machines like MacOS. Linux (thanks god) aren't the only unix-like OS. Moreover, the second solution not using date at all... so... – clt60 Dec 03 '20 at 12:42
  • 1
    The only option for [`date` specified in POSIX](https://pubs.opengroup.org/onlinepubs/9699919799/utilities/date.html) is `-u`. It might still not be fully portable, but that's a reasonable baseline. I'm not saying you have to come up with a solution, just pointing out a fact which might surprise visitors who try to use the information your answer. – tripleee Dec 03 '20 at 12:47
12

if you need substract with timestamp :

timestamp=$(date +%s -d '1 hour ago');
leGabian
  • 121
  • 1
  • 3
4
$ 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

tripleee
  • 175,061
  • 34
  • 275
  • 318
3

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

ray_g
  • 46
  • 5
3

If you need change timezone before subtraction with new format too:

$(TZ=US/Eastern date -d '1 hour ago' '+%Y-%m-%d %H:%M')
Kenan Duman
  • 154
  • 1
  • 5
2

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.

Enlico
  • 23,259
  • 6
  • 48
  • 102
Javier Muñoz
  • 732
  • 1
  • 11
  • 30
2

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...

PhiLho
  • 40,535
  • 6
  • 96
  • 134
  • ahh.. i am using shell script, my formula go like this -DATE=`date "+%m/%d/%Y -%H:%M:%S"` and from DATE i want to subtract 1 hour – shamir chaikin May 09 '11 at 08:32
  • 1
    Are you required to stay in shell? An inline awk or perl script could easily subtract an hour and output in your required format. – CoreyStup May 09 '11 at 20:38
-1
DATE=date -1H "+%m/%d/%Y -%H:%M:%S"
S.B
  • 13,077
  • 10
  • 22
  • 49
Skunk
  • 1