Bash relative date (x days ago)

I have a date string that I can parse and format using 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 get a number.

+8
date linux bash
source share
3 answers

You can do date arithmetic:

 DATE=01/02/2010 echo $(( ( $(date +%s) - $(date -d "$DATE" +%s) ) /(24 * 60 * 60 ) )) 
+6
source share

Convert your date and now in seconds, starting from the era, subtract, divide by the number of seconds per day:

 #!/bin/bash ((a = `date -d "Wed Jan 12 02:33:22 PST 2011" +%s`)) ((b = `date +%s`)) echo $(( (ba) / (60*60*24))) 
+4
source share

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

 date -d "`date`-5days" 
+2
source share

Source: https://habr.com/ru/post/651022/


All Articles