Question

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

Was it helpful?

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)))

OTHER TIPS

If you were trying to display this in a rich text box then you could do the following. This assumes you have a rich text box on a form and have delared your string of ordinals. In the load event of the form add the following:

 Dim D As DateTime = CDate(Now.ToString("MM-dd-yyyy"))
   Dim todate As String = D.Day.ToString() + ordinals(D.Day)
        With RichTextBox1
            .SelectionFont = New Font("Lucinda Console", 12)
            .SelectedText = D.Day.ToString()
            .SelectionCharOffset = 5
            .SelectedText = ordinals(D.Day)
        End With

From here you should be able to play with the formatting. However as a caveat this only seems to work with a rich textbox or control derived from one

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top