kensaii
kensaii

Reputation: 324

Calculate 15 minutes ago in shell

I have a time-stamp like 7:00:00, which means 7am.

I would like to write a short command that returns 06:45:00, or simply 06:45, preferably using date command so that I can avoid long shell script. Do you have any elegant solution?

I'm also looking for a 24h format. For example, 12:00:00 - 15 minutes = 11:45:00.

Upvotes: 4

Views: 2920

Answers (2)

Zlemini
Zlemini

Reputation: 4973

On BSD variants Date has a -v flag which can be used to take the current timestamp and display the result of a positive or negative adjustment.

This will subtract 15mins from the current timestamp:

date -v -15M

Upvotes: 1

heemayl
heemayl

Reputation: 42107

With GNU date, use 7:00:00 AM - 15 minutes as d (--date) string :

% date -d '7:00:00 AM - 15 minutes' '+%H:%M'
06:45

+%H:%M sets the output format as HH:MM.

Upvotes: 6

Related Questions