I have used the below commands to find the 30 mins older time but not able to frame commands to add 30 mins.

Sample structure of input.

inputTime=$(echo $dateinput | sed 's,\(.\{4\}\)\(.\{2\}\)\(.\{2\}\)\(.\{2\}\)\(.\{2\}\),\1-\2-\3 \4:\5:,')
param2=`date '+%Y%m%d%H%M%S' --date="$inputTime 30 minutes ago"`
echo $param2

Please let me know the steps to add 30 mins similar to the above one. For the sample dateinput given above the desired output is 20181214205959.

With the direct date command I am able to add and reduce based on the requirement like given below :

Tue Dec 18 20:49:06 PST 2018
$date --date "-30 minutes"
Tue Dec 18 20:18:55 PST 2018
$date --date "+30 minutes"
Tue Dec 18 21:19:01 PST 2018

But my requirement is to get the date from the input which will be given like 20181214202959 which is in YYYYMMDDhhmmss format. This is where I am facing issues. Able to reduce the time using "30 minutes ago" command but not able to get the future value for the given time.


Your code dateinput=20181214202959 inputTime=$(echo $dateinput | sed 's,\(.\{4\}\)\(.\{2\}\)\(.\{2\}\)\(.\{2\}\)\(.\{2\}\),\1-\2-\3 \4:\5:,')

adds spacing to dateinput to set inputTime to 2018-12-14 20:59:39.

You then are running date --date="2018-12-14 20:59:59 30 minutes ago" asking for a particular output format.

If you remove the ago from the request you will ask for the time 30 minutes in the future. The effect of the word ago is to negate the most recent time offset. In general you can list a number of time offsets and these are all added, so you can combine for example a number of minutes and a number of weeks.

$ date  --date="2018-12-14 20:00:00 30 minutes ago 1 week"
Fri Dec 21 19:30:00 PST 2018
$ date  --date="2018-12-14 20:00:00 30 minutes 1 week"
Fri Dec 21 20:30:00 PST 2018
$ date  --date="2018-12-14 20:00:00 30 minutes 1 week ago"
Fri Dec  7 20:30:00 PST 2018
  • Thanks buddy, tried hell lot of things but missed to try this :) – Sekar Ramu Dec 19 '18 at 6:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.