Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Subtract 1 hour from date in UNIX shell script

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"`
like image 729
shamir chaikin Avatar asked May 09 '11 08:05

shamir chaikin


People also ask

How do you subtract time in Unix?

1 Answer. You can use date to convert to timestamp which is seconds, subtract the seconds and then convert back to HH:MM:SS .

How do I subtract a day from a date in Linux?

Set it to the number of days that you want to subtract. Very slight improvement to the command - date --date="${dataset_date} -${date_diff} day" +%Y-%m-%d.

How do I echo date and time in Linux?

Sample shell script to display the current date and time #!/bin/bash now="$(date)" printf "Current date and time %s\n" "$now" now="$(date +'%d/%m/%Y')" printf "Current date in dd/mm/yyyy format %s\n" "$now" echo "Starting backup at $now, please wait..." # command to backup scripts goes here # ...

What is date command in UNIX with examples?

date command is used to display the system date and time. date command is also used to set date and time of the system. By default the date command displays the date in the time zone on which unix/linux operating system is configured. You must be the super-user (root) to change the date and time.


8 Answers

The following command works on recent versions of GNU date:

date -d '1 hour ago' "+%m/%d/%Y -%H:%M:%S"
like image 66
dogbane Avatar answered Oct 01 '22 06:10

dogbane


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
like image 20
jm666 Avatar answered Oct 01 '22 07:10

jm666


if you need substract with timestamp :

timestamp=$(date +%s -d '1 hour ago');
like image 29
leGabian Avatar answered Oct 01 '22 08:10

leGabian


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

like image 44
ray_g Avatar answered Oct 01 '22 06:10

ray_g


$ 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

like image 45
Elangovan Pandiyan Avatar answered Oct 01 '22 08:10

Elangovan Pandiyan


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

like image 37
PhiLho Avatar answered Oct 01 '22 06:10

PhiLho


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

$(TZ=US/Eastern date -d '1 hour ago' '+%Y-%m-%d %H:%M')
like image 24
Kenan Duman Avatar answered Oct 01 '22 06:10

Kenan Duman


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.

like image 42
Javier Muñoz Avatar answered Oct 01 '22 07:10

Javier Muñoz