Bash relative date (x days ago)
I have a date string that I am able to parse and format with the date
comm开发者_如何学编程and 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.
Use date itself as date value for date. Example 5 days ago:
date -d "`date`-5days"
You can do some date arithmetics:
DATE=01/02/2010
echo $(( ( $(date +%s) - $(date -d "$DATE" +%s) ) /(24 * 60 * 60 ) ))
Convert your date and now into seconds since the epoch, subtract, divide by the number of seconds in a day:
#!/bin/bash
((a = `date -d "Wed Jan 12 02:33:22 PST 2011" +%s`))
((b = `date +%s`))
echo $(( (b-a) / (60*60*24)))
精彩评论