12

In a bash script, if I have a number that represents a time, in the form hhmmss (or hmmss), what is the best way of subtracting 10 minutes?

ie, 90000 -> 85000

6 Answers 6

25

This is a bit tricky. Date can do general manipulations, i.e. you can do:

date --date '-10 min'

Specifying hour-min-seconds (using UTC because otherwise it seems to assume PM):

date --date '11:45:30 UTC -10 min'

To split your date string, the only way I can think of is substring expansion:

a=114530
date --date "${a:0:2}:${a:2:2}:${a:4:2} UTC -10 min"

And if you want to just get back hhmmss:

date +%H%M%S --date "${a:0:2}:${a:2:2}:${a:4:2} UTC -10 min"
2
  • Thanks for the reply. So to clarify...the time is not the present time...it is a five or six digit number passed into the script as a parameter...would this approach work for this? Sep 9, 2010 at 17:53
  • @RogerMoore yes, that's why I use --date. It has to be written as "hh:mm:ss UTC" because otherwise date won't be able to misinterpret it, but it seemed to work for your purpose. I'm not sure what happens when you pass in a time in the form of hh:mm:s, you best test it, also around midnight and noon.
    – wds
    Sep 11, 2010 at 11:53
5

why not just use epoch time and then take 600 off of it?

$ echo "`date +%s` - 600"| bc; date 
1284050588
Thu Sep  9 11:53:08 CDT 2010
$ date -d '1970-01-01 UTC 1284050588 seconds' +"%Y-%m-%d %T %z"
2010-09-09 11:43:08 -0500
3
  • Thanks for the reply. The thing is I don't require to do it based on the present time. It is an arbitrary time will be passed into the script as a five or six digit number. Sep 9, 2010 at 17:49
  • 1
    Usually it's a good idea to strip your own styling/features and use a conventional prompt like "$" when posting. (I set PS1='\$ ' before answering these questions so I can still copy-paste.)
    – Roger Pate
    Sep 16, 2010 at 21:33
  • 1
    I would just modernize the 1st line as echo "$(( $(date +%s) - 600))"; date Aug 14, 2018 at 2:33
1

Since you have a 5 or 6 digit number, you have to pad it before doing string manipulation:

$ t=90100
$ while [ ${#t} -lt 6 ]; do t=0$t; done
$ echo $t
090100
$ date +%H%M%S --utc -d"today ${t:0:2}:${t:2:2}:${t:4:2} UTC - 10 minutes"
085100

Note both --utc and UTC are required to make sure the system's timezone doesn't affect the results.

For math within bash (i.e. $(( and ((), leading zeros will cause the number to be interpreted as octal. However, your data is more string-like (with a special format) than number-like, anyway. I've used a while loop above because it sounds like you're treating it as a number and thus might get 100 for 12:01 am.

1

My version of bash doesn't support -d or --date as used above. However, assuming a correctly 0-padded input, this does work

$ input_time=130503 # meaning "1:05:03 PM"

# next line calculates epoch seconds for today's date at stated time
$ epoch_seconds=$(date -jf '%H%M%S' $input_time '+%s')

# the 600 matches the OP's "subtract 10 minutes" spec. Note: Still relative to "today"
$ calculated_seconds=$(( epoch_seconds - 600 )) # bc would work here but $((...)) is builtin

# +%H%M%S formats the result same as input, but you can do what you like here
$ echo $(date -r $calculated_seconds '+%H%M%S')

# output is 125503: Note that the hour rolled back as expected.
0

For MacOS users you can do the following:

$(date -v -10M +"%H:%M:%S")

Date time without a specific format:

$(date -v -10M)

For non-macOS users:

Date time without a specific format:

date --date '-10 min'
0

try this

date +%H%M%S -d "- 10 min"

is ok for me

1
  • 1
    Thank you for your interest in contributing to the Stack Overflow community. This question already has quite a few answers—including one that has been extensively validated by the community. Are you certain your approach hasn’t been given previously? If so, it would be useful to explain how your approach is different, under what circumstances your approach might be preferred, and/or why you think the previous answers aren’t sufficient. Can you kindly edit your answer to offer an explanation? Sep 21, 2023 at 0:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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