Bash 从今天开始计算 x 个月

Bash calculate x months back from todays date

我正在使用这种日期格式

20130702

并想计算 15 个月前。

我目前正在使用

date -v 10m

使用我当前的日期格式如何创建一个 if 语句来检查日期是否不晚于 10 个月

谢谢大家!!

这很简单:

date -d '20130702 - 15 months'
Mon Apr  2 00:00:00 BST 2012

如果你想保持相同的格式:

date -d '20130702 - 15 months' +%Y%m%d
20120402

WRT给你if语句,你可以先转换成纪元:

#! /bin/bash

date="20130702"
date_epoch=$( date -d "20130702" +%s)
_10months_ago=$(date -d 'now -10 months' +%s)

if [ "$date_epoch" -lt "$_10months_ago" ];then
    echo "$date was before last 10 months"
else
    echo "$date is within last 10 montsh"
fi