Date Format: "*st" "*nd" "*rd"

Jonno

New Member
Joined
Oct 29, 2002
Messages
47
Morning All,

Has anyone found a way to automatically assign the correct suffix to dates?

For example, the custom date format 'ddd dd mmm yy' results in "Fri 11 June 04" for today, but is it possible to add the "th" to acheive '11th'?

The other requirements would be "st" "nd" and "rd".

Thanks!

Jonno.
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Re: Date Format: "*st" "*nd" "*rd&q

It is not clear whether you want to display your date this way in another cell, or in the cell the date exists. If you want it displayed in the same cell, it is not clear whether the date is manually entered, or rather is returned from a formula in that cell. You did not give any cell address or range of cells where you want this to take place. There is a lot of information missing from your post.

Two possibilities playing the most likely odds - -

Formula approach:
Your date is in cell A1 and you want to display it in another cell with that special format:
=TEXT(A1,"ddd d"""&VLOOKUP(DAY(A1),{1,"st";2,"nd";3,"rd";4,"th";21,"st";22,"nd";23,"rd";4,"th";31,"st"},2)&""" mmm yy")


VBA approach:
You want the format to take place in that same cell, not involving other cells:

Again, assuming A1, right click on your sheet tab, left click on View Code, and paste the following procedure into the large white area that is the worksheet module. Press Alt+Q to return to the worksheet.

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address <> "$A$1" Then Exit Sub
If Not IsDate(Target.Value) Then Exit Sub
Dim DateSuffix As String
Select Case Day(Target.Value)
Case 1, 21, 31
DateSuffix = "st"
Case 2, 22
DateSuffix = "nd"
Case 3, 23
DateSuffix = "rd"
Case 4 To 30
DateSuffix = "th"
End Select
Application.EnableEvents = False
Target.Value = Format(Target.Value, "ddd d") & DateSuffix & Format(Target.Value, " mmm yy")
Application.EnableEvents = True
End Sub


Be advised, in either case, you may lose functionality with those cells for calculating any date math operations you were planning on, which is something else you did not mention, but which you should be aware of because the cell values are essentially text and no longer dates.
 
Upvote 0
Re: Date Format: "*st" "*nd" "*rd&q

Modifying Tom's VBA approach, this should keep it functionally as a date:

Private Sub Worksheet_Change(ByVal Target As Range)
If Not IsDate(Target.Value) Then Exit Sub
Dim DateSuffix As String
Target.NumberFormat = ""
Select Case Day(Target.Value)
Case 1, 21, 31
DateSuffix = "\s\t"
Case 2, 22
DateSuffix = "\n\d"
Case 3, 23
DateSuffix = "\r\d"
Case 4 To 30
DateSuffix = "\t\h"
End Select
Application.EnableEvents = False
Target.NumberFormat = "ddd d" & DateSuffix & " mmm yy"
Application.EnableEvents = True
End Sub
 
Upvote 0
Re: Date Format: "*st" "*nd" "*rd&q

Thank you both for your solutions. The date formatting works just fine. Superb!
 
Upvote 0

Forum statistics

Threads
1,223,533
Messages
6,172,883
Members
452,486
Latest member
standw01

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top