Problem

You might need to get a timestamp of yesterday in any script.

Analysis

Do not think about any scripting and converting dates. Just use the old good ‘date’ command.

Solution

This script should be self explanatory:



#!/bin/bash
set -o verbose on
date
date --date=now
date --date=today # same thing
date --date='3 seconds'
date --date='3 seconds ago'
date --date='4 hours'
date --date='tomorrow'
date --date='1 day'
date --date='1 days'
date --date='yesterday'
date --date='1 day ago'
date --date='1 days ago'
date --date='1 week'
date --date='1 fortnight'
date --date='1 month'
date --date='1 year'
Timestamp of yesterday using ‘date’ command
Tagged on:     

One thought on “Timestamp of yesterday using ‘date’ command

  • 23.01.2019 at 10:43
    Permalink

    thanks adding a loop to the day ago really helps!!!

    echo $(date +%Y-%m-%d -d “4 day ago”

    Reply

Leave a Reply to NICK FARROW Cancel reply

Your email address will not be published.