在vb脚本中将日期转换为字符串

时间:2013-06-24 12:50:15

标签: vbscript

我的日期类似于 2013年6月24日,我希望在vb脚本中只输出 6月24日等月份名称和日期。

1 个答案:

答案 0 :(得分: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