1

The GNU date command can output date computed from relative items such as -1 hour or 1 month ago. It can also output date from different input format (for instance calendar date, seconds since epoch, etc...).

Is it possible to combine both a date format and a relative time to compute a new date from a given date using the GNU§ date utility?

Something like:

date_epoch=`date "+%s"`
date --date="@$date_epoch -1 month"

The second command gives an "incorrect date" error...

4

1 回答 1

3

你可以做到,当然:

$ date -d"$(date -d@$date_epoch) -1 month" "+%Y-%m-%d"
2013-09-16

来自:

$ date_epoch=$(date "+%s")
$ date -d@$date_epoch
Wed Oct 16 23:46:50 CEST 2013

你可以使用它“硬编码”:

$ date -d"Wed Oct 16 23:46:50 CEST 2013 -1 month"
Mon Sep 16 23:46:50 CEST 2013

或使用变量:

$ date -d"$(date -d@$date_epoch) -1 month"
Mon Sep 16 23:46:50 CEST 2013
于 2013-10-16T21:44:57.503 回答