Reputation: 153
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
Upvotes: 4
Views: 45315
Reputation: 51
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
Upvotes: 5
Reputation: 1237
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
Upvotes: 1
Reputation: 153
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.....
Upvotes: 10
Reputation: 363
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
Upvotes: 0
Reputation: 363
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
Upvotes: 0