Вопрос

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?

Это было полезно?

Решение

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.

Другие советы

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)

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
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top