4

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

Mike Harris
  • 143
  • 1
  • 1
  • 7

5 Answers5

9

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

20150302110727

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.....

Mike Harris
  • 143
  • 1
  • 1
  • 7
  • While I chose this as my preferred method, be warned that reference to the lib `System.Text.StringBuilder` depends on .NET Framework 3.5 – Adam Cox Mar 03 '22 at 20:49
  • This currently produces 12 hour format for pm (e.g. 05 for 5pm). – Sum None Jul 18 '23 at 23:55
5

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
dta_phx
  • 51
  • 1
  • 1
  • The inconvenient with this solution tho is that it is able to return the date formatted in only one way. If for instance from another function is needed to format it differently, suppose that in a place you need the following date format: yyyyMMddhhmmss and somewhere else you need this other: yyyy-MM-dd hh.mm.ss and elsewhere you need just yyyy-MM-dd and somewhere else you need just HH:mm:ss. With your approach basically you need to write a different function for each formatting cause it hasn't any versatility. – willy wonka Aug 07 '23 at 01:02
1

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

function getDateFormatedWithDash(DateVal)
    rtnDateStr = year(DateVal)
    m=month(DateVal)
    d=day(DateVal)
    h=Hour(DateVal)
    Min=Minute(DateVal)
    sec=second(DateVal)

    if month(DateVal)<10 then
        m="0"&month(DateVal)
    end if

    if day(DateVal)<10 then
        d="0"&day(DateVal)
    end if

    if Hour(DateVal)<10 then
        h="0"&Hour(DateVal)
    end if  

    if Minute(DateVal)<10 then
        Min="0"&Minute(DateVal)
    end if  

    if second(DateVal)<10 then
        sec="0"&second(DateVal)
    end if


    rtnDateStr = rtnDateStr&"-"&m&"-"&d&" "&h&":"&Min&":"&sec
    getDateFormatedWithDash=rtnDateStr
end function
Gil Allen
  • 1,169
  • 14
  • 24
0

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
   CASE TRUE
     add0 = "0" & testIn
   Case Else
     add0 = testIn
  End Select      
End Function    
Bagger
  • 373
  • 1
  • 3
  • 9
0

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
Bagger
  • 373
  • 1
  • 3
  • 9