简体   繁体   English

计算特定日期起12个月前的日期

[英]Calculate a date 12 months ago from a specific date

I'm trying to calculate a date 12 months ago from specific input date. 我正在尝试从特定的输入日期算起12个月前的日期。

Example: 例:

In the Cell A5 the user input a date (dd/mm/yyyy) , and in the Cell A7 i have the following formula: 用户在单元格A5中输入日期(dd / mm / yyyy) ,在单元格A7我具有以下公式:

=MONTH(A5)-12

But this formula doesn't work, and i don´t know how to get the date 12 months ago. 但是这个公式不起作用,而且我不知道如何获得12个月前的日期。 Any question post on comments. 有关评论的任何问题帖子。

You can do this: 你可以这样做:

=DATE(YEAR(A5),MONTH(A5)-12,DAY(A5))

Or since 12 months is one year: 或者因为12个月是一年:

=DATE(YEAR(A5)-1,MONTH(A5),DAY(A5))

Per this Microsoft documentation , the best way to do this is with the EDATE function . 根据此Microsoft文档 ,最好的方法是使用EDATE函数

=EDATE(A5, -12)

(Then format the cell as a Date) (然后将单元格格式化为日期)

Note that there are some differences between this approach and the one Scott recommended . 请注意,这种方法与Scott建议的方法之间存在一些差异。 In particular, consider that if your original value is a leap day (February 29th), then subtracting 12 months will fall into a year where there is no leap day. 特别要考虑的是,如果您的原始值是a日(2月29日),则减去12个月将属于没有a日的一年。 Scott's approach will return March 1st, where the EDATE function will land on February 28th. Scott的方法将于3月1日返回, EDATE函数将于2月28日登陆。

The same thing can be seen when adding/subtracting any arbitrary number of months near the end of a long month. 在长月底之前添加/减去任意月数时,可以看到相同的结果。 For example, adding one month to March 31st should give April 30th with the EDATE approach, but will give May 1st when adding values to date parts. 例如,在3月31日前加上一个月应使用EDATE方法将日期定为4月30日,但在对日期部分添加值时将在5月1日。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM