-3

I have date like 6/24/2013 , i want to get only month name and date like June 24th as output in vb script.

Venaikat
  • 197
  • 2
  • 5
  • 20

1 Answers1

3

Use two functions to deal with the two (sub) problems - name of month, ordinal of number - separately:

Option Explicit

Dim n
For n = -2 To 10
    WScript.Echo fmtDate(DateAdd("d", n, Date))
Next

Function fmtDate(dtX)
  fmtDate = MonthName(Month(dtX)) & " " & ordinal(Day(dtX))
End Function

' !! http://stackoverflow.com/a/4011232/603855
Function ordinal(n)
  Select Case n Mod 10
    case 1    : ordinal = "st"
    case 2    : ordinal = "nd"
    case 3    : ordinal = "rd"
    case Else : ordinal = "th"
  End Select
  ordinal = n & ordinal
End Function

output:

June 22nd
June 23rd
June 24th
June 25th
June 26th
June 27th
June 28th
June 29th
June 30th
July 1st
July 2nd
July 3rd
July 4th

Update:

(Hopefully) improved version of ordinal():

Function ordinal(n)
  Select Case n Mod 31
    case  1, 21, 31 : ordinal = "st"
    case  2, 22     : ordinal = "nd"
    case  3, 23     : ordinal = "rd"
    case Else       : ordinal = "th"
  End Select
  ordinal = n & ordinal
End Function
Ekkehard.Horner
  • 38,498
  • 2
  • 45
  • 96