is there a way to subtract a month from a date or at least 30 days in unix.
example:
[yyy-mm-dd] - 30 days
2011-07-23 - 30 days
By the way, the date can be any date depending on the user input.
Thanks.
For an arbitrary date,
$ date -d "2011-07-23 - 1 month" "+%F"
2011-06-23
$ date -d "2011-07-23 - 30 days" "+%F"
2011-06-23
$ date -d "2011-08-23 - 1 month" "+%F"
2011-07-23
$ date -d "2011-08-23 - 30 days" "+%F"
2011-07-24
This is GNU date
Without GNU date, you can fall back to perl. The Time::Piece and Time::Seconds module should be available in perl 5.12
perl -MTime::Piece -MTime::Seconds -e '
print "date\t-1 month\t-30 days\n";
while (@ARGV) {
my $t = Time::Piece->strptime(shift, "%Y-%m-%d");
print $t->ymd, "\t";
print $t->add_months(-1)->ymd, "\t";
$t -= 30*ONE_DAY;
print $t->ymd, "\n";
}
' 2011-07-23 2011-08-23
date -1 month -30 days
2011-07-23 2011-06-23 2011-06-23
2011-08-23 2011-07-23 2011-07-24
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With