Sub topic 11
Date formats and date codes
This chapter assumes you know that Excel stores a date as a serial number and shows it through a number format. It also assumes you can open Format Cells and choose Custom. Here you will learn the codes that control how a date looks.
Built-in date formats
Format Cells offers two built-in date formats, Short Date and Long Date. A format marked with an asterisk follows the Windows regional settings, so it looks different on a computer set to another region. The stored value is the same on every computer. For a file that must look identical everywhere, write a custom format.
The date codes
A custom date format is built from codes. Each code stands for one part of the date.
| Code | Meaning | Shows for 5 Jan 2024 |
|---|---|---|
| d | Day, no leading zero | 5 |
| dd | Day, two digits | 05 |
| ddd | Weekday, abbreviated | Fri |
| dddd | Weekday, full name | Friday |
| m | Month number | 1 |
| mm | Month number, two digits | 01 |
| mmm | Month name, abbreviated | Jan |
| mmmm | Month name, full | January |
| mmmmm | First letter of month | J |
| yy | Year, two digits | 24 |
| yyyy | Year, four digits | 2024 |
Any other characters you type, such as hyphens, slashes, commas, and spaces, are shown as written. These formats produce the following results:
yyyy-mm-ddThis shows 2024-01-05.
dddd, mmmm d, yyyyThis shows Friday, January 5, 2024.
Month or minute?
The code m means month in a date and minute in a time. Excel decides by position. It reads m or mm as minutes only when it comes right after h or hh, or right before ss. In every other position it reads the code as a month.
flowchart TD
A[Format contains m or mm] --> B{Right after h or hh?}
B -- Yes --> C[Minutes]
B -- No --> D{Right before ss?}
D -- Yes --> C
D -- No --> E[Month]The diagram mermaid block below shows the same decision.
Changing the look versus changing the value
A date format changes only the display. =DATE(2024,1,1) returns 45292, and a date format makes it show as a date. The formula bar still holds the full value. If you want the date as text, for example to build a label, use TEXT:
=TEXT(A1,"yyyy-mm-dd")The result of TEXT is text, not a date, so you cannot use it in date arithmetic.
Common errors
- A date shows as a number such as 45292. The cell has General or Number format. Apply a date format.
- Day and month are swapped. A format such as
m/d/yyyyord/m/yyyywas chosen, or the regional setting differs. Useyyyy-mm-ddto avoid ambiguity. - TEXT returns the wrong result. Format codes inside TEXT can depend on the language of the Excel installation. Another language version may expect different letters for year and day.
- Cells show
####. The column is too narrow for the date. Widen the column.
Exercise
Which custom format code displays the date 5 January 2024 as "Fri 05 Jan 2024"?
Show answer
ddd dd mmm yyyy. The code ddd gives the abbreviated weekday, dd the two-digit day, mmm the abbreviated month, and yyyy the four-digit year. The spaces are literal characters.