I have date like 6/24/2013 , i want to get only month name and date like June 24th as output in vb script.
Asked
Active
Viewed 1,748 times
-3
-
I tried using cstr(date()) but its returning 6/24/2013 – Venaikat Jun 24 '13 at 12:54
-
1Is this question about VBScript or VB.net? They're different languages. Also, do you have that date as a string, or as a date value? – Ansgar Wiechers Jun 24 '13 at 13:33
-
I tried using this code MonthName(Month(Now())) and date am hard coding ,so i got jun 24th – Venaikat Jun 24 '13 at 13:42
-
Would you awfully mind answering the questions you're being asked? – Ansgar Wiechers Jun 24 '13 at 13:49
1 Answers
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

Christian Sirolli
- 45
- 10

Ekkehard.Horner
- 38,498
- 2
- 45
- 96