简体   繁体   English

如何使用 excel 公式计算 yyyymm 格式的两个日期时间值之间的差异?

[英]How can I calculate the difference between two date time values in the format yyyymm using excel formula?

I use the following formula,我使用以下公式,

INT(LEFT(A4,4) & TEXT((DATEVALUE(MID(A4,6,3) & " 1")),"mm"))=INT($C$1-1)

INT(LEFT(A4,4) & TEXT((DATEVALUE(MID(A4,6,3) & " 1")),"mm")) this part returns date in format yyyymm. INT(LEFT(A4,4) & TEXT((DATEVALUE(MID(A4,6,3) & " 1")),"mm"))这部分以 yyyymm 格式返回日期。

$C$1's value is inputted by a user in the format yyyymm. $C$1 的值由用户以 yyyymm 格式输入。

I want to compare it with the latter part but it fails if the value of $C$1 is set to 202101, for instance, please find the picture attached.我想和后面的部分进行比较,但是如果$C$1的值设置为202101,则失败,例如,请查找附图。 I want the final output to be true in this situation.我希望最终的 output 在这种情况下是正确的。

例子

Is there a way to modify the latter part/better to approach this problem?有没有办法修改后半部分/更好地解决这个问题?

I solved my question by the following minor tweak in my current formula,我通过在当前公式中进行以下小调整解决了我的问题,

INT(LEFT(A4,4) & TEXT((DATEVALUE(MID(A4,6,3) & " 1")),"mm")) = IF(RIGHT($C$1,2)="01", INT($C$1-89), INT($C$1-1))

The latter part of the formula that is INT(LEFT(A4,4) & TEXT((DATEVALUE(MID(A4,6,3) & " 1")),"mm")) returns me a yyyymm.公式的后半部分INT(LEFT(A4,4) & TEXT((DATEVALUE(MID(A4,6,3) & " 1")),"mm"))返回一个 yyyymm。 So if $C$1 was 202101 it would first check the two characters from right if it is "01", if it is true then it would subtract 89 from it hence resulting 202012 .因此,如果 $C$1 是202101 ,它将首先检查右边的两个字符,如果它是“01”,如果它是真的,那么它会从中减去 89 从而得到202012 And it works for all other cases I think.它适用于我认为的所有其他情况。 Obviously, if there need be to see 202011 instead of 202012 , then the formula would change to,显然,如果需要看到202011而不是202012 ,那么公式将变为,

    INT(LEFT(A4,4) & TEXT((DATEVALUE(MID(A4,6,3) & " 1")),"mm")) = IF(RIGHT($C$1,2)="01", INT($C$1-90), INT($C$1-2))

and so on..等等..

This answer is based on the comments by the OP to the original question.该答案基于 OP 对原始问题的评论。 OP requested that 1 be subtracted from the date 202101 to give 202012 as the answer. OP 要求从日期 202101 中减去 1 以给出 202012 作为答案。

Assuming the string date is in C3 and the value to subtract is in D3, use the following formula in E3:假设字符串日期在 C3 中,要减去的值在 D3 中,在 E3 中使用以下公式:

=IF((RIGHT(C3,2)-D3)>0,LEFT(C3,4)&RIGHT("0"&(RIGHT(C3,2)-D3),2),RIGHT("000"&LEFT(C3,4)-1,4)&RIGHT("0"&RIGHT(C3,2)+12-D3,2))

This should work provided:这应该可以工作:

  • It is not year 0这不是第 0 年
  • No more than 12 months is being subtracted减去不超过 12 个月

POC

UPDATE更新

Since the question has new information.由于问题有新信息。 Use the following formula in B2在 B2 中使用以下公式

=A2=--IF((RIGHT(C1,2)-1)>0,LEFT(C1,4)&RIGHT("0"&(RIGHT(C1,2)-1),2),RIGHT("000"&LEFT(C1,4)-1,4)&RIGHT("0"&RIGHT(C1,2)+11,2))

If you are unsure if the value of A2 is going to be a number stored as text or an actual number, you can tweak the above formula with the following:如果您不确定 A2 的值是存储为文本的数字还是实际数字,您可以使用以下内容调整上述公式:

=--A2=--IF((RIGHT(C1,2)-1)>0,LEFT(C1,4)&RIGHT("0"&(RIGHT(C1,2)-1),2),RIGHT("000"&LEFT(C1,4)-1,4)&RIGHT("0"&RIGHT(C1,2)+11,2))

POC II

Yes, you can, in two steps.是的,你可以,分两步。

Step one, convert the text value to the date第一步,将文本值转换为日期

=DATE(MID(A1;1;4);MID(A1;5;2)+1;0)

when A1 is the date text value.当 A1 是日期文本值时。 It returns the last day of the month, which is very convenient for most calculations.它返回一个月的最后一天,这对于大多数计算来说非常方便。

Step two第二步

Once you get dates out of text values, do the difference, eg date2-date1 to get the output in days.一旦你从文本值中得到日期,做差异,例如 date2-date1 得到 output 以天为单位。

To convert the resulting date back to text string, use text formatting要将生成的日期转换回文本字符串,请使用文本格式

=TEXT(C1;"yyyymm")

The actual formatting string depends on you regional settings of your system.实际的格式化字符串取决于您系统的区域设置。 See excel function TEXT help https://support.microsoft.com/en-us/office/text-function-20d5ac4d-7b94-49fd-bb38-93d29371225c for details. See excel function TEXT help https://support.microsoft.com/en-us/office/text-function-20d5ac4d-7b94-49fd-bb38-93d29371225c for details.

In your question you perhaps were looking not for the difference, but for the comparison.在您的问题中,您可能不是在寻找差异,而是在寻找比较。 In that case you could simply convert the text to the value as在这种情况下,您可以简单地将文本转换为值

=VALUE(A1)

and then compare the values.然后比较值。

=IF(VALUE(A2)>VALUE(B2);A2;B2)

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

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