I am working with a timestamp which is in the following format which I am told is know as ISO 8601 :


I would like to be able to calculate the timestamp both 15 minutes and 30 minutes before the current timestamp.

UPDATE: I need the output to be in the same format as the original timestamp

I have some code which retrieves the timestamp at any given time, but how can I get a script to calculate the timestamp at a given number of minutes before the current timestamp- for this example I would like to know both 15 minutes and 30 minutes before.

Any help appreciated.


The first point of call would be

man date

There you may find the details on setting time with a relative offset, as in

date --date '-15 minutes 2016-04-27T14:14:47.836Z'

It also offers details about how to format the output.

  • Thanks for the response. I tried this and it works in terms of calculating the time, but the output is in a different format to the timestamp- I need the output to be in the same format as the original timestamp, I've updated my original question with this info. – neilH Apr 27 '16 at 14:55
  • if you read further down the man page for date it shows you how to format the output, of the top of my head it could be: date --date '-15 minutes 2016-04-27T14:14:47.836Z' "+%Y-%m-%dT%H:%I:%S" but I may be mixing things up, so do man date and check – Cwissy Apr 27 '16 at 15:12
  • Thanks, the formatting code you suggested worked as I wished thanks. The issue I have now is for some reason the '-15 minutes 2016-04-27T14:14:47' part is taking off many hours! I created a new question for this here unix.stackexchange.com/questions/279791/… – neilH Apr 28 '16 at 17:08

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.