date format function to display date as “Jan 13 2014”

若如初见. 提交于 2019-11-26 22:10:52

问题


Is there any function to display date in the format mmm-dd-yyyy in VBScript? eg. today's date as Jan 22 2014?

I tried using function

FormatDateTime(Now(), 2)

I got it as 16 January 2014.

Is there any function/format to get it as Jan 16 2014?


回答1:


By using a .NET Stringbuilder - for all your formatting needs - you get the most bang for the buck:

Option Explicit

Class cFormat
  Private m_oSB
  Private Sub Class_Initialize()
    Set m_oSB = CreateObject("System.Text.StringBuilder")
  End Sub ' Class_Initialize
  Public Function formatOne(sFmt, vElm)
    m_oSB.AppendFormat sFmt, vElm
    formatOne = m_oSB.ToString()
    m_oSB.Length = 0
  End Function ' formatOne
  Public Function formatArray(sFmt, aElms)
    m_oSB.AppendFormat_4 sFmt, (aElms)
    formatArray = m_oSB.ToString()
    m_oSB.Length = 0
  End Function ' formatArray
End Class ' cFormat

Dim oFmt : Set oFmt = New cFormat
WScript.Echo oFmt.FormatOne("Today: {0:MMM dd yyyy}", Date())
WScript.Echo oFmt.FormatOne("Today: {0:yyyy-MM-dd [MMMM]}", Date())

Output:

cscript 21279700.vbs
Today: Jan 22 2014
Today: 2014-01-22 [Januar]

Have a look here for background.




回答2:


FormatDate formats a date according to the format configured in the system's regional settings. If you want a custom date format using VBScript builtins, you'd do it like this:

WScript.Echo MonthName(Month(Now), True) & " " & Day(Now) & " " & Year(Now)



回答3:


Below function will help which is returning in "dd-mmm-yyyy hh:mm:ss" format. You can customize the format according to your need.

Function timeStampForLogging(t)
    Dim Months

    Months = Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", _ 
                   "Jul", "Aug", "Sep", "Oct", "Nov", "Dec") 


    timeStampForLogging =  Right("0" & Day(t),2)& "-" & _
    Months(Month(t)-1)  & "-" & _
    Year(t) & " " & _  
    Right("0" & Hour(t),2) & ":" & _
    Right("0" & Minute(t),2) & ":" & _
    Right("0" & Second(t),2) 
End Function


来源:https://stackoverflow.com/questions/21279700/date-format-function-to-display-date-as-jan-13-2014

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!