Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash relative date (x days ago)

Tags:

date

linux

bash

I have a date string that I am able to parse and format with the date command from a bash script.

But how can I determine how many days ago this date was from my script? I would like to end up with a number.

like image 482
Dennis Thrysøe Avatar asked Jan 13 '11 10:01

Dennis Thrysøe


People also ask

How do you subtract dates in bash?

Set it to the number of days that you want to subtract. Very slight improvement to the command - date --date="${dataset_date} -${date_diff} day" +%Y-%m-%d.


1 Answers

Use date itself as date value for date. Example 5 days ago:

date -d "`date`-5days"
like image 95
Andreas WP Avatar answered Sep 29 '22 14:09

Andreas WP