2013-06-24 132 views
-3

轉換日期爲字符串我有約會像2013年6月24日,我想只有一個月的姓名和日期一樣6月24日在VB腳本輸出。在VB腳本

+0

我試過使用cstr(日期()),但它的返回2013/6/24 – Venaikat

+1

這是關於VBScript或VB.net的問題?他們是不同的語言。此外,您是否將該日期作爲字符串或日期值? –

+0

我試過使用這個代碼MonthName(Month(Now()))和日期是硬編碼,所以我得到jun​​ 24th – Venaikat

回答

3

使用兩個函數來處理兩個(子)問題 - 每個月的姓名,號碼的順序 - 分別:

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 

輸出:

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 

更新:

(希望)改進版的序數():

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