Written by Allen Wyatt (last updated January 16, 2021)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365
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:
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.
Program Successfully in Excel! John Walkenbach's name is synonymous with excellence in deciphering complex technical topics. With this comprehensive guide, "Mr. Spreadsheet" shows how to maximize your Excel experience using professional spreadsheet application development tips from his own personal bookshelf. Check out Excel 2013 Power Programming with VBA today!
Excel allows you to perform all sorts of calculations using dates. A good example of this is using a formula to figure ...
Discover MoreExcel is great when it comes to working with dates and times. You can even do math on dates. One such easy manipulation ...
Discover MoreExcel can easily store dates. If you want to increment a date by one month, there are a number of ways you can accomplish ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
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.
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.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2024 Sharon Parq Associates, Inc.
Comments