Using a Single Digit for a Year

Written by Allen Wyatt (last updated December 20, 2024)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365


7

Bruce needs to display dates that represent a product's "Best By" date. The required format is mmddy, two-digit month, two-digit day, and one-digit year. So, September 28, 2021, would read 09281. Bruce cannot seem to come up with a custom format that uses only one digit for a year, though.

It is not difficult to come up with a custom format for this, it is impossible. Excel's custom formatting allows for either two or four digits for years, but not one or three. Because of this, you need to rely on either a formula or a user-defined function (macro).

Let's look at the formula methods, first. The idea is to use the equivalent of a custom format (in the TEXT function) for the portions that can be represented properly, and then add to that a single digit for the year. Either of these formulas will handle that:

=TEXT(A1,"mmdd") & RIGHT(TEXT(A1,"yy"),1)
=TEXT(A1,"mmdd") & RIGHT(YEAR(A1),1)

If you prefer, you could create a single-line user-defined function, in this manner:

Function DateCode(DateVal As Date) As String
    DateCode = Format(DateVal, "mmdd") & Right(Year(DateVal), 1)
End Function

This function essentially does, in macro form, what the second formula above does. To use the function, you would simply place it in a cell, like so:

=DateCode(A1)

Remember that the result of any of these approaches (formula or UDF) is a text string. So, for instance, you could combine the results with other text, in this manner:

="Expires: " & TEXT(A1,"mmdd") & RIGHT(TEXT(A1,"yy"),1)
="Good through " & TEXT(A1,"mmdd") & RIGHT(YEAR(A1),1)
="Use before " & DateCode(A1)

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (9971) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365.

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

A Quick-and-Dirty Word Count

Word provides a tool that counts the number of words in a document. Here's an alternative method of calculating the ...

Discover More

Allowing Only Comments In a Document

Develop a document that is to be reviewed by a group of people, and you may want to protect it in some way. One way you ...

Discover More

Relative References within Named Ranges

Excel is usually more flexible in what you can reference in formulas than is immediately apparent. This tip examines some ...

Discover More

Excel Smarts for Beginners! Featuring the friendly and trusted For Dummies style, this popular guide shows beginners how to get up and running with Excel while also helping more experienced users get comfortable with the newest features. Check out Excel 2013 For Dummies today!

More ExcelTips (ribbon)

Deciphering a Coded Date

It is no secret that Excel allows you to work with dates in your worksheets. Getting your information into a format that ...

Discover More

Determining If a Year is a Leap Year

Need to figure out if a given year is a leap year? It's not as easy as you think! This tip provides a plethora of ways ...

Discover More

Calculating Week-Ending Dates

When working with dates, you may need to figure out all the dates on which weeks end in a given year. There are several ...

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}] (all 7 characters, in the sequence shown) 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 4 + 5?

2024-12-21 14:40:36

Zsamot

@jamies
I agree with you. What is the purpose of this requested date format? To confuse consumers? or to be able to sell a product that is over ten years past the expiry date?
Also, there are product that have very long shelf life, for example plastic drums and other containers, but they do age, and should be replaced after expiry, often 5 to 10 years after production. Such date format would have been useless for those.


2024-12-21 01:08:25

Rick Schubert

What's wrong with MOD(YEAR(date),10)?


2024-12-20 07:30:48

jamies

Then again,
perhaps it is wise to consider -
Should you use a single digit for a year ?

indeed, for those who were involved in any computing activities way way way back in the year 0
OK, 2000 AD,
Think about all the problems reported, and encountered, that were considered to be the "Year 2K" effects -

the Effect being of all those computer systems set to only use 2 digits for the year,
so computer systems calculating things like interest on a loan taken out in 1998 as having 97 years interest due in the 3rd year of the borrowing.
and the people born in the early 20th century being told that, as a minor, they needed their parents permission to do things, such as not be at school.

Oh - and please remember Windows is coded to handle dates entered as 2 digit years as being either in the 20th century, or the 21st century depending on the value being over (is it 35) so a date with a year entered as 40 precedes one entered as 30.


2022-11-19 14:03:46

Tomek

My suggestion is an user defined function:
```

Public Function DateCode(DateVal As Date) As String

DateCode = Format(DateVal, "mmdd") & (DatePart("yyyy", DateVal) Mod 10)

End Function
```
You can add a check whether argument is a valid date. The result is a string, but can be easily modified to a number; however it cannot be a date.

Also the code generated will be off by one day for dates between Jan 1 and Feb 28, in the year 1900. BTW, Excel accepts Feb. 29, 1900 as a valid date while 1900 was not a leap year; the code will be off by one day for that invalid date too. This shouldn't matter for Bruce though.


2021-01-18 12:43:05

David Bonin

I use and would love to see a universal date format of yyyy-mm-dd.
This puts folders and files in chronological order even where the system is not seeing it as a date.

Using dashes instead of slashes allows this date format to be used in file names. I use this as a simple method of version control.

It also has the advantage of conforming to ISO 8601. If nothing else, conformance helps eliminate the "mine is better than yours" argument.

Lastly, this format seems universal enough to be recognized all over the world. It's worked so far for the people on the four continents I work with.


2021-01-16 12:17:02

Don

VBA also has a nice little DatePart function. In the immediate window, this input "? right(DatePart("yyyy","01/16/2021"),1)" produces a result of "1".


2021-01-16 10:46:48

Will

I use and would love to see a universal date format of yyyy/mm/dd. This puts folders and files in chronological order even where the system is not seeing it as a date.

Seems terribly logical.


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.