How to subtract a time from date time format in shell? [duplicate] - bash

This question already has answers here:
How to subtract 5 minute from date
(2 answers)
Closed 4 years ago.
I have a particular date and time with me in shell. Now I need to subtract 7 hours from that particular date and time.
eg.
2018-03-20 21:00:00 -> 2018-03-20 14:00:00
2018-03-20 06:00:00 -> 2018-03-19 23:00:00
I have both date and time in different strings as well.
How to write this in shell (v4.1)?

Some implementations of date understand time arithmetics like "-7 hours":
#!/bin/bash
format='+%Y-%m-%d %H:%M:%S'
Test () {
date=$1
time=$2
expect=$3
d=$(date "$format %Z" -d "$date $time")
plus7=$(date "$format" -d "$d -7 hours")
if [[ $plus7 == $expect ]] ; then
echo ok
else
echo $plus7
fi
}
Test 2018-03-20 21:00:00 '2018-03-20 14:00:00'
Test 2018-03-20 06:00:00 '2018-03-19 23:00:00'
Tested with date (GNU coreutils) 8.25.

You can use the POSIX arithmetic operator in conjunction with date -d# to subtract 7 hours (7 * 3600 seconds) from any given date, e.g.
$ date -d#$(($(date -d"2018-03-20 21:00:00" +%s) - 7 * 3600))
Tue Mar 20 14:00:00 CDT 2018

Related

Get saturday date for the given input date using bash script

By using the below command, it will return the last saturday date.
date +"%b-%d-%Y" -d "last saturday"
Sep-01-2018
I want to pass input date as parameter, which should return the last saturday's date in bash script.
Aug-08-2018 -----> Aug-04-2018
Jun-04-2018 -----> Jun-02-2018
Get a negative number that will be the number of days to subtract. We use 13, because Saturday is 6, and 6 + 7 = 13. This will get us the Saturday one or two weeks ahead. Then we modulo 7, to ensure it is NEXT Saturday, then subtract 7 to make it LAST Saturday. Then we put that diff into the date string:
$ date_str="Aug-08-2018"
$ diff=$(( (13 - $(date +"%u" -d ${date_str})) % 7 - 7))
$ date -d "${date_str} ${diff} days"
Sat Aug 4 00:00:00 EDT 2018

how to add number of days to custom date in bash shell script [duplicate]

In GNU with the command date I can do it:
date -d "+4 day"
datei=20130101
i=5
date -d "$datei +$i day"
But i like know:
how can i do it in Solaris?
with the date command
Tcl has a good free-form date scanner, if you have Tcl installed (try which tclsh). A shell function:
tcldate() {
d=${1:-now} # the date string
f=${2:-%c} # the output format
echo "puts [clock format [clock scan {$d}] -format {$f}]" | tclsh
}
In action on an ancient Solaris 8 box with bash 2.03 and tcl 8.3.3
$ tcldate
Tue Jul 23 13:27:17 2013
$ i=4
$ tcldate "$i days"
Sat Jul 27 13:27:34 2013
$ tcldate "$i days" "%Y-%m-%d"
2013-07-27
$ tcldate "20130101 + $i days" "%Y-%m-%d"
2013-01-05
This even handles daylight savings transitions:
$ tcldate "2014-03-09 00:30 + 1 hour" "%D %T %Z"
03/09/14 01:30:00 EST
$ tcldate "2014-03-09 00:30 + 2 hour" "%D %T %Z"
03/09/14 03:30:00 EDT
$ tcldate "2013-11-03 00:30 + 1 hour" "%D %T %Z"
11/03/13 01:30:00 EDT
$ tcldate "2013-11-03 00:30 + 2 hour" "%D %T %Z"
11/03/13 01:30:00 EST

How to subtract 5 minute from date

I have this data:
`date +%Y-%m-%d`" 00:00:00"
that return 2015-10-08 00:00:00
I would like cancel 5 minute:
2015-10-07 23:55:00
Many thanks
You need to subtract 5 minutes from a known point in time:
$ date -d "00:00:00 today"
Thu Oct 8 00:00:00 EDT 2015
$ date -d "00:00:00 today -5 minutes"
Wed Oct 7 23:55:00 EDT 2015
You just need to add your format string.
There's more than one way to subtract a value from the current time, although this should match the format shown in your question:
date -d "-5 min" "+%Y-%m-%d %H:%M:%S"
Result:
2015-10-08 15:26:13

How do I get the month one calendar month ago in bash? [duplicate]

This question already has answers here:
Using `date` command to get previous, current and next month
(5 answers)
Closed 8 years ago.
I have been using the command:
date --date='1 months ago' +%b
To get the month name of the month it was a month ago, but have realised today as it is the 31st that this command actually gives me the month name it was 4 weeks ago.
Is there any way to get the calendar month that it was 1 month ago, or indeed n months ago as I can see that the discrepancy will be greater as the number of months is longer.
Date calculations that depend on the number of days in the month are tricky. A hybrid approach, using month numbers and a lookup table, will probably work best.
months=("" Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec)
echo ${months[$(date +%m) - 1 ]}
[[ $(date +%d) == "31" ]] && date -d'-31 day' +%b || date -d'-1 month' +%b
test with today:
kent$ date
Thu Jul 31 17:34:27 CEST 2014
kent$ [[ $(date +%d) == "31" ]] && date -d'-31 day' +%b || date -d'-1 month' +%b
Jun
try this one line
#if the month before 30 days is the same of the actual month ,then return the month before 31 days
[[ `date --date='30 day ago' +%b` == `date +%b` ]] && echo `date --date='31 day ago' +%b` || echo `date --date='30 day ago' +%b`

How to get epoch time in shell script (for ksh)?

How to get epoch time in shell script (for ksh)?
I am interested in getting epoch time for the start of day (so e.g. now is July 28th, 2011 ~ 14:25:00 EST, I need time at midnight).
If you have GNU date,
epoch=$( date -d 00:00 +%s )
Otherwise, if you have tclsh,
epoch=$( echo 'puts [clock scan 00:00]' | tclsh )
Otherwise,
epoch=$( perl -MTime::Local -le 'print timelocal(0,0,0,(localtime)[3..8])' )
ksh's printf '%(fmt)T' supports time calculating. For example:
$ printf '%T\n' now
Mon Mar 18 15:11:46 CST 2013
$ printf '%T\n' '2 days ago'
Sat Mar 16 15:11:55 CST 2013
$ printf '%T\n' 'midnight today'
Mon Mar 18 00:00:00 CST 2013
$

Resources