Bash相对日期(x天前)

时间:2021-08-22 19:45:03

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

我有一个日期字符串,可以从bash脚本中解析和格式化日期命令。

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

但是,我要如何确定这个日期是多少天前从我的脚本?我想以一个数字结束。

3 个解决方案

#1


7  

You can do some date arithmetics:

你可以做一些日期算术:

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

#2


4  

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)))

#3


2  

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

使用日期本身作为日期的日期。示例5天前:

date -d "`date`-5days"

#1


7  

You can do some date arithmetics:

你可以做一些日期算术:

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

#2


4  

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)))

#3


2  

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

使用日期本身作为日期的日期。示例5天前:

date -d "`date`-5days"