Home > Blockchain >  Subtract hours from date object in bash
Subtract hours from date object in bash

Time:07-11

I need to find a time X hours before the previous midnight, thus I would like to subtract X hours from a date object.

Example

# Finding the previous midnight
date -d "yesterday 23:59:59"
Mon Jul 11 00:00:00 CEST 2022

What I want I would like to find the date X hours before this midnight

x=4
Mon Jul 10 20:00:00 CEST 2022

CodePudding user response:

You can use this date command:

date -d "today 0 -4 hours"

Here:

  • today - gets midnight date-time for today's date
  • -4 hours subtracts 4 hours from midnight

CodePudding user response:

It seems one can just write the following:

date --date 'yesterday 23:59:59 CEST -4 hours'
  • Related