How to get milliseconds
Moderators: JRL, Dorian (MJT support)
How to get milliseconds
All,
I need to create my own timestamp including milliseconds. I haven't found a funtion for that. Currently, I am extracting the milliseconds with this workaround (last three digits from TimeStamp variable):
DateStamp>c:\temp_file.tmp,
ReadFile>c:\temp_file.tmp,TimeStamp
DeleteFile>c:\temp_file.tmp
...
Can someone tell me how to directly assign milliseconds to a variable? That would be great.
Thanks in advance
George
I need to create my own timestamp including milliseconds. I haven't found a funtion for that. Currently, I am extracting the milliseconds with this workaround (last three digits from TimeStamp variable):
DateStamp>c:\temp_file.tmp,
ReadFile>c:\temp_file.tmp,TimeStamp
DeleteFile>c:\temp_file.tmp
...
Can someone tell me how to directly assign milliseconds to a variable? That would be great.
Thanks in advance
George
Here's one way. VBScript "Timer" function gives the seconds since midnight in a form that usually runs to 5 or 6 decimal places. You just need to round out to 3 decimal places. Then make sure you have three decimal places at the end. If you only have 1 or 2 you can pad zeros on the end to get 3.
Your method is much more efficient.
Code: Select all
VBSTART
VBEND
Hour>hh
Min>mn
Sec>ss
//This chunk of code give you the milliseconds portion of the time.
VBEval>timer,SecondsSinceMidnight
Let>SecondsSinceMidnight={(Round(%SecondsSinceMidnight%*1000))/1000}
Separate>SecondsSinceMidnight,.,TimePart
Length>TimePart_2,len
If>len=3
Else
If>len=2
Let>TimePart_2=%TimePart_2%0
Else
Let>TimePart_2=%TimePart_2%00
EndIf
EndIf
//////////////////////////////////////////////////////////////////////
mdl>%hh%:%mn%:%ss%.%TimePart_2%
Here's another way. Someone good with vbscript might be able to do most of this in one line.
Maybe there's a RegEx method.
Maybe there's a RegEx method.
Code: Select all
VBSTART
VBEND
VBEval>timer,SecsSinceMidNight
VBEval>Round (%SecsSinceMidNight%, 3),RoundedSecs
VBEval>FormatNumber(%RoundedSecs%, 3),PaddedSecs
Separate>PaddedSecs,.,millisecs
mdl>Millisecs_2
Hi gknopf,
Thanks for sharing that with us... its simpler than other methods I've used to generate a timestamp so this has been added to snippets:
Also, thanks JRL for your method.
Marcus, are there any simpler methods available?
I know we have these commands:
Sec>Seconds
Min>Minutes
Hour>Hour
And at first, I thought why not add this...
FSec>Fractional_Seconds
...but that seems silly when what I really want is the whole thing into a variable looking like this:
2011-02-10:23:47:35:153
...and that is what gknopf's workaround method already gives us.
To improve on that, a potential new MS command could do the above in just one line and might be called:
DateStampToVar>myVar
Not a huge need but it would be convienient none-the-less. Maybe consider adding to the Wish List? Just a thought...
Thanks and take care
Thanks for sharing that with us... its simpler than other methods I've used to generate a timestamp so this has been added to snippets:
Code: Select all
DateStamp>%TEMP_DIR%~temp_file.tmp,
ReadFile>%TEMP_DIR%~temp_file.tmp,TimeStamp
DeleteFile>%TEMP_DIR%~temp_file.tmp
// var TimeStamp now contains data in the following format:
// 2011-02-10:23:24:46:662 -
// and there is a trailing space after the hyphen above
// the following line gets rid of the trailing " - "
StringReplace>TimeStamp, - ,,TimeStamp
MDL>TimeStamp
Marcus, are there any simpler methods available?
I know we have these commands:
Sec>Seconds
Min>Minutes
Hour>Hour
And at first, I thought why not add this...
FSec>Fractional_Seconds
...but that seems silly when what I really want is the whole thing into a variable looking like this:
2011-02-10:23:47:35:153
...and that is what gknopf's workaround method already gives us.
To improve on that, a potential new MS command could do the above in just one line and might be called:
DateStampToVar>myVar
Not a huge need but it would be convienient none-the-less. Maybe consider adding to the Wish List? Just a thought...
Thanks and take care
jpuziano
Note: If anyone else on the planet would find the following useful...
[Open] PlayWav command that plays from embedded script data
...then please add your thoughts/support at the above post -
Note: If anyone else on the planet would find the following useful...
[Open] PlayWav command that plays from embedded script data
...then please add your thoughts/support at the above post -

Last method I mentioned could be shortened to:
Code: Select all
VBSTART
VBEND
VBEval>FormatNumber(timer, 3),PaddedSecs
Separate>PaddedSecs,.,millisecs
mdl>Millisecs_2
Thanks JRL,
I discovered that with gknopf's method, I still had a %CRLF% at the end of the DateTimeStamp value but a modification to the StringReplace> line cleaned that up.
Note also that gknopf's method produces a DateTimeStamp (unless we take extra steps to chop off the Date) and your method produces a TimeStamp (no date)...
Mystery: I ran the above code several times and most of the time, DateTimeStamp had an earlier value than TimeStamp... even though Timestamp is generated first. Does anyone know why?
Open Challenge: Can anyone get a TimeStamp or a DateTimeStamp value into an MS variable in less lines of code than the above? Fame, glory and Reputation Points to anyone who can improve on either of the above.
Take care
I discovered that with gknopf's method, I still had a %CRLF% at the end of the DateTimeStamp value but a modification to the StringReplace> line cleaned that up.
Note also that gknopf's method produces a DateTimeStamp (unless we take extra steps to chop off the Date) and your method produces a TimeStamp (no date)...
Code: Select all
VBSTART
VBEND
//6 lines (not including VBSTART/VBEND) to get TimeStamp into a variable
Hour>hh
Min>mn
Sec>ss
VBEval>FormatNumber(timer, 3),PaddedSecs
Separate>PaddedSecs,.,millisecs
Let>TimeStamp=%hh%:%mn%:%ss%.%millisecs_2%
//4 lines to get DateTimeStamp into a variable
DateStamp>%TEMP_DIR%~temp_file.tmp,
ReadFile>%TEMP_DIR%~temp_file.tmp,DateTimeStamp
DeleteFile>%TEMP_DIR%~temp_file.tmp
StringReplace>DateTimeStamp, - %CRLF%,,DateTimeStamp
MDL>%TimeStamp%%CRLF%%DateTimeStamp%
Open Challenge: Can anyone get a TimeStamp or a DateTimeStamp value into an MS variable in less lines of code than the above? Fame, glory and Reputation Points to anyone who can improve on either of the above.
Take care
jpuziano
Note: If anyone else on the planet would find the following useful...
[Open] PlayWav command that plays from embedded script data
...then please add your thoughts/support at the above post -
Note: If anyone else on the planet would find the following useful...
[Open] PlayWav command that plays from embedded script data
...then please add your thoughts/support at the above post -

- Marcus Tettmar
- Site Admin
- Posts: 7395
- Joined: Thu Sep 19, 2002 3:00 pm
- Location: Dorset, UK
- Contact:
This should give you milliseconds of current time:
VBEval>Fix((Timer() - ((Hour(Now) * 3600) + (Minute(Now) * 60) + Second(Now))) * 1000),milliseconds
So this will provide a timestamp to jpuziano's format:
VBEval>Year(now) & "-" & Right("0" & Month(now),2) & "-" & Right("0" & Day(now),2) & ":" & Right("0" & Hour(now),2) & ":" & Right("0" & Minute(now),2) & ":" & Right("0" & Second(now),2) & ":" & Fix((Timer() - ((Hour(Now) * 3600) + (Minute(Now) * 60) + Second(Now))) * 1000),timestamp
All in one - albeit long - line.
VBEval>Fix((Timer() - ((Hour(Now) * 3600) + (Minute(Now) * 60) + Second(Now))) * 1000),milliseconds
So this will provide a timestamp to jpuziano's format:
VBEval>Year(now) & "-" & Right("0" & Month(now),2) & "-" & Right("0" & Day(now),2) & ":" & Right("0" & Hour(now),2) & ":" & Right("0" & Minute(now),2) & ":" & Right("0" & Second(now),2) & ":" & Fix((Timer() - ((Hour(Now) * 3600) + (Minute(Now) * 60) + Second(Now))) * 1000),timestamp
All in one - albeit long - line.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
That is excellant Marcus, thanks. I popped that huge line into a Subroutine and called it like this:
So with the above Subroutine in a script, it gives us the ability to load any variable with a DateTimeStamp value using just the following two lines:
And even simpler, if we are fine with just choosing one known variable that will hold the value, we could do this:
That way, we could just use the following short one-liner at any point in a script where we need load the current DateTimeStamp value into the variable called DateTimeStamp
Take care
Code: Select all
SRT>DateTimeStamp
VBEval>Year(now) & "-" & Right("0" & Month(now),2) & "-" & Right("0" & Day(now),2) & ":" & Right("0" & Hour(now),2) & ":" & Right("0" & Minute(now),2) & ":" & Right("0" & Second(now),2) & ":" & Fix((Timer() - ((Hour(Now) * 3600) + (Minute(Now) * 60) + Second(Now))) * 1000),VBDateTimeStamp
Let>%DateTimeStamp_Var_1%=VBDateTimeStamp
END>DateTimeStamp
Let>VarName={"myDateTimeStamp"}
GoSub>DateTimeStamp,VarName
MDL>myDateTimeStamp
Code: Select all
Let>VarName={"myDateTimeStamp"}
GoSub>DateTimeStamp,VarName
Code: Select all
SRT>Load_Var_DateTimeStamp
VBEval>Year(now) & "-" & Right("0" & Month(now),2) & "-" & Right("0" & Day(now),2) & ":" & Right("0" & Hour(now),2) & ":" & Right("0" & Minute(now),2) & ":" & Right("0" & Second(now),2) & ":" & Fix((Timer() - ((Hour(Now) * 3600) + (Minute(Now) * 60) + Second(Now))) * 1000),DateTimeStamp
END>Load_Var_DateTimeStamp
GoSub>Load_Var_DateTimeStamp
MDL>DateTimeStamp
Code: Select all
GoSub>Load_Var_DateTimeStamp
jpuziano
Note: If anyone else on the planet would find the following useful...
[Open] PlayWav command that plays from embedded script data
...then please add your thoughts/support at the above post -
Note: If anyone else on the planet would find the following useful...
[Open] PlayWav command that plays from embedded script data
...then please add your thoughts/support at the above post -

All,
your feedback was very inspiring. On my pursuit to find a smart and efficient solution I thought about tapping into the Win32 API with this 2-liner:
The function GetTickCount does privide milliseconds since the last system startup.
A better function would be "GetLocalTime" from Kernel32.dll. There you would get the correct millisecond value from the timer but I have no clue on how to extract it. If someone does, thanks for sharing in advance.
Still, for the extraction of ms Marcus' solution is unmatched.
Greetings from Munich
George
your feedback was very inspiring. On my pursuit to find a smart and efficient solution I thought about tapping into the Win32 API with this 2-liner:
Code: Select all
LibFunc>kernel32.dll,GetTickCount,MSCounter
MidStr>%MSCounter%,{(length(%MSCounter%)-2)},3,MilliSeconds
MessageModal>ms since system start: %MSCounter% %crlf%ms extracted: %MilliSeconds%
A better function would be "GetLocalTime" from Kernel32.dll. There you would get the correct millisecond value from the timer but I have no clue on how to extract it. If someone does, thanks for sharing in advance.
Still, for the extraction of ms Marcus' solution is unmatched.
Greetings from Munich
George