Please Note: This article is written for users of the following Microsoft Excel versions: 2007 and 2010. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Deciphering a Coded Date.

Deciphering a Coded Date

by Allen Wyatt
(last updated May 12, 2017)

17

Luis receives information in which dates are coded such that years, months, and days are replaced with a single character each. For each field, the numbers 1 to 9 are used and after that letters, from a=10, b=11, ... v=31. For example, the date code 'bc2' means b=11 (the year 2011), c=12 (the month), and day=2. Luis wonders if a function can be devised to replace the coded date with a common date format, such as dd/mm/yyyy.

There are actually several ways you could go about solving this problem. One way is to set up "equivalence tables" within a worksheet, where the left column includes a code character and the right indicates the numeric value that is associated with that character. You could then fashion a formula that uses VLOOKUP to find the values and convert the results into a date.

As an example, create you equivalence table in some unused cells to the right of your data. In my case, I put the table in columns P and Q. In column P I put the code characters, 1 through 9 and a through z. (Make sure you precede the digits 1 through 9 with an apostrophe so they are stored as text rather than as numbers.) In column Q I put the numbers 1 through 35. This entire range (P1:Q35) I then gave a name of DateTable. Here is the formula, then, that will return a decoded date for a coded date stored in cell A1:

=DATE(2000+VLOOKUP(LEFT(A1,1),DateTable,2,0),
VLOOKUP(MID(A1,2,1),DateTable,2,0),VLOOKUP(RIGHT(A1,1),
DateTable,2,0))

Remember that this is a single, continuous formula. Another technique is to bypass the equivalence tables altogether and instead use a formula to do the conversion. The following is an example that will decode a date in cell A1:

=DATE(2000+FIND(LEFT(A1,1),"123456789abcdefghijklmnopqrstuvwxyz"),
FIND(MID(A1,2,1),"123456789abc"),FIND(MID(A1,3,1),
"123456789abcdefghijklmnopqrstuv"))

This formula uses the FIND function to translate from the code character to a value, and then these values are used in the DATE function to return the desired date. Another possible formula relies, instead, on character code values to create the date:

=DATE(2000+CODE(MID(A1,1,1))-87+(CODE(MID(A1,1,1))<58)*39,CODE(MID(A1,2,1))-87+(CODE(MID(A1,2,1))<58)*39,CODE(MID(A1,3,1))-87+(CODE(MID(A1,3,1))<58)*39)

Finally, you could create a user-defined function to return the decoded date. The following is just a simple example; it looks at each character and converts it to a numeric value that is then used with the DateSerial function to create an Excel date serial number:

Function DecodeDate(datecode As String)
    Const X = "123456789abcdefghijklmnopqrstuvwxyz"
    Dim D As Integer
    Dim M As Integer
    Dim Y As Integer

    Application.Volatile
    D = InStr(X, Right(datecode, 1))
    M = InStr(X, Mid(datecode, 2, 1))
    Y = 2000 + InStr(X, Left(datecode, 1))
    DecodeDate = DateSerial(Y, M, D)
End Function

It should be pointed out, as well, that regardless of the approach you use, there is an inherent flaw in your date codes. The year uses the code values 1 through 9 and a through z. This means that the date code can be one of 35 possible values. When added to the year 2000 (the base year for how you described the code), that means that the maximum year value that can be coded is 2035. Any date after that year will not work with this coding.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (12423) applies to Microsoft Excel 2007 and 2010. You can find a version of this tip for the older menu interface of Excel here: Deciphering a Coded Date.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He  is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Understanding ASCII and ANSI Characters

Two of the most common character coding schemes used in computers go by the acronyms ASCII and ANSI. This tip explains a bit ...

Discover More

Margins Incorrect when Printing

If you notice that the margins on a printout aren't the same as those you have set within Word, there could be any number of ...

Discover More

Repeating Column Information on Each Page

When your table occupies lots of pages, you may want to have information in a particular column repeated on each page. Word ...

Discover More

Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!

More ExcelTips (ribbon)

Specifying Different Weekends with NETWORKDAYS

The NETWORKDAYS worksheet function can be used to easily determine the number of work days (Monday through Friday) within a ...

Discover More

The Last Business Day

Many businesses need to know when the last business day of the month occurs. This tip discusses several ways you can ...

Discover More

Unique Date Displays

Need to print an elapsed date in a strange format? It's easier to do than may appear at first glance. Here's a discussion on ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 7 - 0?

2017-05-15 09:52:29

Shandor

Excellent example Neil, thanks! And if Microsoft would use their ginormous programming skills and market influence to modernize date handling, text handling, and other daily tasks--they could really empower knowledge workers and keep their own profitability up.


2017-05-14 21:25:22

Neil

Agree Shandor. Is a left over from the 60s when every byte was precious. This mentality still creeps into software and causes unnecessary pain when, as inevitably happens the software is used for longer than expected or the scope of the application changes. A personal example I had was when a batch number was programmed with 2 digits on the basis that there would never be more than 100 batches. However the functionality proved very useful and people started putting in three digit numbers which caused immense problems (the programmer had not constrained the input field but instead the program dropped the trailing digits). What saved a few cents in storage space caused heaps of rework and confusion.


2017-05-12 10:35:56

Shandor

It's disappointing to see the efforts of so many smart people wasted on silly issues like coded dates. Dates are dates and always will be, so the business software that has imposed coded dates on human beings should be taken out and horsewhipped! We now have boundless compute power and infinite storage, so there's no need for coded dates, or year abbreviations like the one that led to the Y2K crisis, or almost crisis. Using underscores in data column names is another outdated habit that causes grief for users. It's time for a mass movement like "Nerds For People, Not Machines!" to rise up and demand an end to busywork, and for better, smarter software. Of course some will say that they have software that demands these workarounds, but the Cloud can replace any software, and very affordably. And that's my rant.


2017-01-29 12:26:43

Willy Vanhaelen

In the last paragraph of this tip: "... Any date after that year (2035) will not work with this coding." is not correct.

Except for the first formula, you can extend the range of years with 26 upper case letters since CODE and FIND in the formulas and InStr in the UDF are case sensitive. So you can code till the year 2061.


2017-01-27 20:25:51

Harry S

It was a nice item to pass some time
Why not have all days as string
yyyy/mm/dd about 20,000 per megabyte
or
yyyy/mm/dd hh:mm:ss

or even the form of some sql searches
yyyymmddhhmmssmsms

Why these are sortable as strings
Most countries can recognise the format
a simple class can extract Y m d h m s and translate to any country or local localformat


2017-01-27 15:37:27

Harry S

' to be different

Function DecodeDate3(DS$) As Date
Dim Ci%, TT() As Byte, BadDate As Boolean

' from 2 byte char representation to 1 byte ASCI
TT() = StrConv(DS, vbFromUnicode)

' ubound is 2 we hope ..3 char and zero based arrays
For Ci = 0 To UBound(TT)
' 0..9 is 48 ..57 to go to 0 1 2 3
' A... is 64 + to go to 10 11 12 as in Hexadecimal 0..F
' a... is 96 + to go to 10 11 12
Select Case TT(Ci)

Case Is > 96: TT(Ci) = TT(Ci) - 87
Case Is > 64: TT(Ci) = TT(Ci) - 55
Case 48 To 57: TT(Ci) = TT(Ci) - 48
Case Else
BadDate = True

End Select
Next Ci
If BadDate Then
DecodeDate3 = Int(Now) ' or some error message
Else
DecodeDate3 = DateSerial(2000 + TT(0), TT(1), TT(2))
End If

End Function


2016-11-24 14:39:33

Peter

Is it possible to return the x leftmost characters of the vlookup return value?
Such as
=Left(vLOOKUP(C5;DATA1!A2:A225;DATA1!G2:G225,0))

or
=LOOKUP(C5;DATA1!A2:A225;left(DATA1!G2:G225,4))

I keep getting an error of bad syntax

Thanks


2013-01-12 15:46:11

Peter Atherton

The CODE function refers to the ASCII value of every letter on the keyboard as in

Text Code Char Function
0 48 0
9 57 9
A 65 A
Z 90 Z
a 97 a
z 122 z

The inverse is the CHAR Function.


2013-01-06 11:38:12

Juan

What's the meaning of the CODE function?


2013-01-05 21:11:35

Peter Atherton

If the dates are in Hex then the parts would need to be separated as in a normal date; say in the format y,m,d

If this can be arranged then the following would work:

=DATE(HEX2DEC(LEFT(A16,FIND(",",A16)-1))+2000,HEX2DEC(MID(A16,FIND(",",A16)+1,1)),HEX2DEC(MID(A16,FIND(",",A16,FIND(",",A16)+1)+1,1234)))
This gives as far as I've tested correct dates from 1/1/2000 (0,1,1)to 31/12/2099 (63,c,1f) when it would be easier to change the base date to 2100


2013-01-05 18:51:40

Peter Atherton

The troube with using code values is the data must be entered in Lower case, Upper case values produce the wrong dates.

The best solution IMHO is to change the Find Function publshed by Allan to Search as in:

=DATE(2000+SEARCH(LEFT(C1,1),"123456789abcdefghijklmnopqrstuvwxyz"),SEARCH(MID(C1,2,1),"123456789abc"),SEARCH(MID(C1,3,1),"123456789abcdefghijklmnopqrstuv"))

Anyway Luis mentioned fields in the original post and if this translates to columns I think that the dates use Hexadecimal notation and will sometimes have more than one character per date part e.g. 10 for year 2016. If so we should be using HEX2DEC.

Any thoughts


2013-01-05 15:01:52

Michael Avidan - MVP

OK, got it to work now.

It should read:

=DATE(2000+MOD(CODE(LEFT(A1,1))-48,39),MOD(CODE(MID(A1,2,1))-48,39),MOD(CODE(RIGHT(A1,1))-48,39))

Michael Avidan
“Microsoft®” MVP – Excel
ISRAEL


2013-01-05 14:23:36

Michael Avidan - MVP

@Willy,

Thanks for bringing this to my attantion.

My "new" formula is now not much shorten than the formula that bypasses the equivalence tables.

=DATE(39*(LEFT(M1)<"9")+CODE(LEFT(M1))+1913,IF(MID(M1,2,1)>"9",CODE(MID(M1,2,1))-87,MID(M1,2,1)),IF(RIGHT(M1)>"9",CODE(RIGHT(M1))-87,RIGHT(M1)))

Still, Jan-Willems formula returns an error (or I'm doing something wrong).

Michael Avidan
“Microsoft®” MVP – Excel
ISRAEL


2013-01-05 13:46:36

Willy Vanhaelen

Of course cdc and bdk return an error. There is no 'd' in the lookup string. The middle character is the month. 'd' is thirteen and there are only 12 months in a year :-)


2013-01-05 11:07:31

Michael Avidan - MVP

@Jan-Willem.

Please check your proposed formula for dates like: cdc and bdk.

Michael Avidan
“Microsoft®” MVP – Excel
ISRAEL


2013-01-05 10:56:38

Michael Avidan - MVP

To my opinion the formula can be much shorter:

=DATE(CODE(LEFT(A1))+1913,CODE(MID(A1,2,1))-87,IF(RIGHT(A1)>"9",CODE(RIGHT(A1))-87,RIGHT(A1)))

By the way the formula:

=DATE(2000+FIND(LEFT(A1,1),"123456789abcdefghijklmnopqrstuvwxyz"),FIND(MID(A1,2,1),"123456789abc"),FIND(MID(A1,3,1),"123456789abcdefghijklmnopqrstuv"))

returns an Error for: cdc and bdk.

Michael Avidan
“Microsoft®” MVP – Excel
ISRAEL


2013-01-05 06:51:12

Jan-Willem Lankhaar

The third solution can be shortened to

=DATE(2000+MOD(CODE(LEFT(A1,1)-48,39),MOD(CODE(MID(A1,2,1))-48,39),MOD(CODE(RIGHT(A1,1))-48,39)


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.