简体   繁体   中英

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.

$C$1's value is inputted by a user in the format 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. I want the final output to be true in this situation.

例子

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. 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 . 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,

    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 requested that 1 be subtracted from the date 202101 to give 202012 as the answer.

Assuming the string date is in C3 and the value to subtract is in D3, use the following formula in 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
  • No more than 12 months is being subtracted

POC

UPDATE

Since the question has new information. Use the following formula in 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=--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. 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.

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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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