VBScript formatting date/time

Use this forum if you have problems with a hMailServer script, such as hMailServer WebAdmin or code in an event handler.
Post Reply
User avatar
SorenR
Senior user
Senior user
Posts: 3620
Joined: 2006-08-21 15:38
Location: Denmark

VBScript formatting date/time

Post by SorenR » 2019-04-25 02:02

Code: Select all

Function sprintf(sFmt, aData)
   Dim g_oSB
   Set g_oSB = CreateObject("System.Text.StringBuilder")
   g_oSB.AppendFormat_4 sFmt, (aData)
   sprintf = g_oSB.ToString()
   g_oSB.Length = 0
   Set g_oSB = Nothing
End Function
WScript.Echo "sprintf 24H format =>", sprintf( "{0:yyyy-MM-dd HH:mm:ss}", Array(Date()))
WScript.Echo "sprintf 12H format =>", sprintf( "{0:yyyy-MM-dd h:mm:ss tt}", Array(Date()))

NOTE: I have not been able to get the 12H AM PM thingy (tt) to work but that could be due to my Windows being Danish, time WILL show as 12 hour format without actually printing "AM" or "PM" ... :roll:

Code: Select all

sprintf 24H format => 2019-04-25 00:00:00
sprintf 12H format => 2019-04-25 12:00:00
SørenR.

“Those who don't know history are doomed to repeat it.”
― Edmund Burke

User avatar
jimimaseye
Moderator
Moderator
Posts: 8631
Joined: 2011-09-08 17:48

Re: VBScript formatting date/time

Post by jimimaseye » 2019-04-25 08:51

SorenR wrote:
2019-04-25 02:02
NOTE: I have not been able to get the 12H AM PM thingy (tt) to work but that could be due to my Windows being Danish, time WILL show as 12 hour format without actually printing "AM" or "PM" ... :roll:
I think youre right. Isnt it only English that does AM,PM? I thought the rest of the world more or less operates a 24hr clock and only the awkward english speakers do it differently. :mrgreen:
5.7 on test.
SpamassassinForWindows 3.4.0 spamd service
AV: Clamwin + Clamd service + sanesecurity defs : https://www.hmailserver.com/forum/viewtopic.php?f=21&t=26829

estradis
Normal user
Normal user
Posts: 156
Joined: 2014-09-09 10:47

Re: VBScript formatting date/time

Post by estradis » 2019-04-25 10:22

Our idea was to create a converted array from a datetime value and prepare the results in a way we need it in the scripts. Once the array was created, it is easy to build the necessary strings.

Code: Select all

Function GetTimeArray(ts, iDecimals)
	Dim a(6), n, iSeconds, iMilliseconds
	If iDecimals < 0 Then iDecimals = 0
	If iDecimals > 8 Then iDecimals = 8
	n = "00000000"	' <= Leading zeros for array strings
	
	' Workaround for milliseconds (VBScript doesn't know any function for milliseconds!)
	iSeconds = (Hour(ts) * 3600) + (Minute(ts) * 60) + Second(ts)
	iMilliseconds = Timer() - iSeconds
	iMilliseconds = Fix(iMilliseconds * 10 ^ iDecimals)
	
	' As we need only strings, we convert the values here and prepend leading zeroes
	' (This increases performance dramatically!)
	a(0) = Right(n & CStr(Year(ts)), 4)
	a(1) = Right(n & CStr(Month(ts)), 2)
	a(2) = Right(n & CStr(Day(ts)), 2)
	a(3) = Right(n & CStr(Hour(ts)), 2)
	a(4) = Right(n & CStr(Minute(ts)), 2)
	a(5) = Right(n & CStr(Second(ts)), 2)
	a(6) = Right(n & CStr(iMilliseconds), iDecimals)
	
	GetTimeArray = a
End Function
Ok, there is no "am"/"pm" in this array, but it shouldn't be that hard to implement it if needed.

User avatar
SorenR
Senior user
Senior user
Posts: 3620
Joined: 2006-08-21 15:38
Location: Denmark

Re: VBScript formatting date/time

Post by SorenR » 2019-04-25 14:37

estradis wrote:
2019-04-25 10:22
Our idea was to create a converted array from a datetime value and prepare the results in a way we need it in the scripts. Once the array was created, it is easy to build the necessary strings.

Code: Select all

Function GetTimeArray(ts, iDecimals)
	Dim a(6), n, iSeconds, iMilliseconds
	If iDecimals < 0 Then iDecimals = 0
	If iDecimals > 8 Then iDecimals = 8
	n = "00000000"	' <= Leading zeros for array strings
	
	' Workaround for milliseconds (VBScript doesn't know any function for milliseconds!)
	iSeconds = (Hour(ts) * 3600) + (Minute(ts) * 60) + Second(ts)
	iMilliseconds = Timer() - iSeconds
	iMilliseconds = Fix(iMilliseconds * 10 ^ iDecimals)
	
	' As we need only strings, we convert the values here and prepend leading zeroes
	' (This increases performance dramatically!)
	a(0) = Right(n & CStr(Year(ts)), 4)
	a(1) = Right(n & CStr(Month(ts)), 2)
	a(2) = Right(n & CStr(Day(ts)), 2)
	a(3) = Right(n & CStr(Hour(ts)), 2)
	a(4) = Right(n & CStr(Minute(ts)), 2)
	a(5) = Right(n & CStr(Second(ts)), 2)
	a(6) = Right(n & CStr(iMilliseconds), iDecimals)
	
	GetTimeArray = a
End Function
Ok, there is no "am"/"pm" in this array, but it shouldn't be that hard to implement it if needed.
Like this ?

Code: Select all

Function sprintf(sFmt, aData)
   With CreateObject("System.Text.StringBuilder")
      .AppendFormat_4 sFmt, (aData)
      sprintf = .ToString()
      .Length = 0
   End With
End Function

Dim CurrTime  : CurrTime = Now()
Dim Elapsed   : Elapsed = Timer()
Dim MilliSecs : MilliSecs = Right("000" & Int((Elapsed - Int(Elapsed)) * 1000), 3)

WScript.Echo "sprintf 24H format #3  =>", sprintf( "{0:yyyy/MM/dd HH:mm:ss}", Array(CurrTime)) & "." & MilliSecs
:mrgreen:
SørenR.

“Those who don't know history are doomed to repeat it.”
― Edmund Burke

estradis
Normal user
Normal user
Posts: 156
Joined: 2014-09-09 10:47

Re: VBScript formatting date/time

Post by estradis » 2019-04-26 01:07

SorenR wrote:
2019-04-25 14:37
--snip--
Like this ?
--snip--
:mrgreen:
:| OOPS, missunderstood!

What I ment was, that the array is created only once and then reused in multiple functions with different formattings. As the string will be built from the parts only by concatenating, the performance increased.

But yes, you're right. Your function results the same.
:lol:

Post Reply