-3

我有像6/24/2013这样的日期,我只想获得像6 月 24日这样的月份名称和日期作为 vb 脚本的输出。

4

1 回答 1

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

更新:

(希望) 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
于 2013-06-24T13:43:23.193 回答