vb.netconverterssuperscriptordinals

Show Ordinals as superscripts


I have the below code for adding ordinals such as st,rd,th and so on..

Private ordinals As String() = New String() {"", "st", "nd", "rd", "th", "th", _
"th", "th", "th", "th", "th", "th", _
"th", "th", "th", "th", "th", "th", _
"th", "th", "th", "st", "nd", "rd", _
"th", "th", "th", "th", "th", "th", _
"th", "st"}

For getting date I'm writing it as follows:

Dim D As DateTime = Me.PresentDate.Value.ToString("MM-dd-yyyy")
Dim todate As String = D.Day.ToString() + ordinals(D.Day)

Result:

5th

But I would like to get the result as shown below

enter image description here


Solution

  • Why not just use the superscript characters in the first place?

    Dim ordinals = {"", "ˢᵗ", "ⁿᵈ", "ʳᵈ", "ᵗʰ", "ᵗʰ", _
                    "ᵗʰ", "ᵗʰ", "ᵗʰ", "ᵗʰ", "ᵗʰ", "ᵗʰ", _
                    "ᵗʰ", "ᵗʰ", "ᵗʰ", "ᵗʰ", "ᵗʰ", "ᵗʰ", _
                    "ᵗʰ", "ᵗʰ", "ᵗʰ", "ˢᵗ", "ⁿᵈ", "ⁿᵈ", _
                    "ᵗʰ", "ᵗʰ", "ᵗʰ", "ᵗʰ", "ᵗʰ", "ᵗʰ", _
                    "ᵗʰ", "ˢᵗ"}
    
    Dim D = DateTime.Now
    Dim todate = D.Day.ToString() + ordinals(D.Day) ' todate = 6ᵗʰ
    

    or create a simple lookup dictionary:

    Dim ordinals = {"", "st", "nd", "rd", "th", "th", _
                    "th", "th", "th", "th", "th", "th", _
                    "th", "th", "th", "th", "th", "th", _
                    "th", "th", "th", "st", "nd", "rd", _
                    "th", "th", "th", "th", "th", "th", _
                    "th", "st"}
    
    Dim supers = "abcdefghijklmnopqrstuvwxyz".Zip("ᵃᵇᶜᵈᵉᶠᵍʰⁱʲᵏˡᵐⁿᵒᵖXʳˢᵗᵘᵛʷˣʸᶻ", AddressOf Tuple.Create) _
                                             .ToDictionary(Function(t) t.Item1, Function(t) t.Item2)
    Dim D = Date.Now
    Dim todate = D.Day.ToString() + String.Join("", ordinals(D.Day).Select(Function(c) supers(c)))