VB Script date formats "YYYYMMDDHHMMSS"

vbscript format date leading zero
vbscript datepart
vbscript convert string to date yyyymmdd
vbscript cdate
vbscript validate date format
asp date format
vbscript format number
vbscript compare time

As title surgest I need to fomat the now () function to display on the format "YYYYMMDDHHMMSS"

I did have a play about trying to split it out but this drops leading zeros that I need to retain

example below mydt was "27/02/2015 13:02:27"

mydt = now() 

MSGBOX Year(mydt)& Month(mydt)& Day(mydt)& Hour(mydt)& Minute(mydt)& second(mydt)

this returns "201522713227"

i need it to return "20150227130227" i could use a if < 10 but there must be a slicker way

Thanks to @Ekkehard.Horner and @Bagger

I have reviewed your advice and have chosen to go with the below, adapted for my needs.

I have chosen this one as it is a lot more useable/adaptable I can swap and change date formats as required.

Dim g_oSB : Set g_oSB = CreateObject("System.Text.StringBuilder")

Function sprintf(sFmt, aData)
   g_oSB.AppendFormat_4 sFmt, (aData)
   sprintf = g_oSB.ToString()
   g_oSB.Length = 0
End Function


Dim dt : dt = now()

WScript.Echo sprintf("{0:yyyyMMddhhmmss}", Array(dt))

This returns the value in required format yyyyMMddhhmmss


If you just require date you would simply change the sprintf

sprintf("{0:yyyyMMdd}", Array(dt))

Just want the time

sprintf("{0:hhmmss}", Array(dt))

and so on.....

Date formatting in VBScript, This tutorial will explain you about the different Date Functions in VBScript with simple examples for your easy and clear understanding. A Function, which returns a specified part of the given input date value: DateSerial: A Function, which returns a valid date for the given year,month and date: FormatDateTime: A Function, which formats the date based on the supplied parameters: IsDate: A Function, which returns a Boolean Value whether or not the supplied parameter is a date: Day

Here is my full method I used to make it nice. Good for parsing the data for SQL.

function getDateFormatedWithDash(DateVal)
    rtnDateStr = year(DateVal)

    if month(DateVal)<10 then
    end if

    if day(DateVal)<10 then
    end if

    if Hour(DateVal)<10 then
    end if  

    if Minute(DateVal)<10 then
    end if  

    if second(DateVal)<10 then
    end if

    rtnDateStr = rtnDateStr&"-"&m&"-"&d&" "&h&":"&Min&":"&sec
end function

VBScript Date Functions: Date Format, DateAdd, and cDate Functions, VBScript Date and Time Functions help the developers to convert date and time from one format to another or to express the date or time value in the format that  This is a format function which is used to convert the Date to some specific format based on the parameters that are supplied to the function. The syntax of this is FormatDateTime(Date,Format). This is a widely used format function

Old post but was looking and stumbled on this. Ended up going with the following example that can also be used as a one-liner...

wscript.echo DateString(now())

Function DateString(dDate)
    DateString = Year(dDate)& right("0" & Month(dDate),2) & right("0" & Day(dDate),2) & right("0" & Hour(dDate),2) & right("0" & Minute(dDate),2) & right("0" & second(dDate),2)
End Function

VBScript - Date and Time Functions, 0 = vbGeneralDate - Default. Returns date: mm/dd/yy and time if specified: hh:mm​:ss PM/AM. 1 = vbLongDate - Returns date: weekday,  If you want your output to be "culture invariant", use myDateFormat() from stian.net's solution. If you just don't like slashes in dates and you don't care about date format in other countries, you can just use. Replace(FormatDateTime(d,2),"/","-")

This works, you could also use regex

mydt = now() 
wscript.echo (Month(mydt))
mm = add0( Month(mydt))
dd = add0( Day(mydt))
hh = add0( Hour(mydt))
mn = add0( Minute(mydt))
ss = add0( second(mydt))

MSGBOX Year(mydt)& mm & dd & hh & mn & ss

Function add0 (testIn)
 Select Case Len(testIn) < 2
     add0 = "0" & testIn
   Case Else
     add0 = testIn
  End Select      
End Function    

VBScript How can I Format Date?, The optional DateFormat argument must use the constant or value from the Date Format Constants table. The FormatDateTime function formats dates and times. date: Required. Any valid date expression (like Date() or Now()) format: Optional. A value that specifies the date/time format to use. Can take the following values: 0 = vbGeneralDate – Default. Returns date: mm/dd/yy and time if specified: hh:mm:ss PM/AM. 1 = vbLongDate – Returns date: weekday, monthname, year; 2 = vbShortDate – Returns date: mm/dd/yy

Here is a regex example

  mydt = now()
Set regEx = New RegExp
  With regEx
  .Pattern = "\b\d\b"
  .IgnoreCase = True
  .Global = True
 End With

wscript.echo  Year(mydt)& _
  regEx.Replace(Month(mydt),"0" Month(mydt)) & _
  regEx.Replace(Day(mydt),"0" & Day(mydt)) &_
  regEx.Replace(Hour(mydt),"0" & Hour(mydt)) &_
  regEx.Replace(Minute(mydt),"0" & Minute(mydt)) & _ 
  regEx.Replace(second(mydt),"0" & second(mydt))

Set RegularExpressionObject = nothing

VBScript >> Functions >> FormatDateTime, There are certain date value formats and TestComplete routines that help handle dates. (See a note below to learn about the format of date values in VBScript.). Classic ASP VBScript A cheat sheet for people who want to format dates using VBScript Use of concatentation to customise date display: <% Response.Write WeekDayName (WeekDay (Now ())) & _ ", " & MonthName (Month (Now ())) & _ " " & Day (Now ()) & ", " & Year (Now ()) %>

VBScript, FormatDateTime. ASP Classic provides FormatDateTime function which allows you to format a valid date or time expression. For date, it allows  Here are a few examples of Format function for Date in Excel VBA to make this function more clear. 1. Format(#24/03/2014#, “Short Date”) would return ’24/03/2014′ 2. Format(#24/03/2014#, “Long Date”) would return ‘March 24, 2014’ 3. Format(#24/03/2014#, “yyyy/mm/dd”) would return ‘2014/03/24’

Custom Date Formatting in VBScript, Date and time is shown by your system settings. 1 - (vbLongDate) Display a date using the long date format specified in your computer's regional settings. Description. CDate. Converts a valid date and time expression to the variant of subtype Date. Date. Returns the current system date. DateAdd. Returns a date to which a specified time interval has been added. DateDiff. Returns the number of intervals between two dates.

FormatDateTime - function of language VBScript, but, that did not really give me what I needed. In practical terms, this means today's date, March 21, 2011 would be displayed at 2011-03-21. In parsing operations, custom date and time format strings can be used with the DateTime.ParseExact, DateTime.TryParseExact, DateTimeOffset.ParseExact, and DateTimeOffset.TryParseExact methods. These methods require that an input string conforms exactly to a particular pattern for the parse operation to succeed.

  • @Ekkehard.Horner - likely somthing you will be able to assist with?
  • see if stackoverflow.com/a/9641534/603855 helps.