The command wmic os get localdatetime
will give an output like
LocalDateTime
20160926085318.630000+120
you can than place the output in a var or directly split it to multiple variables.
For a single line output add the switch /value
to the command above. Output will then look like this:
LocalDateTime=20160926085649.867000+120
To show the concept behind the function idea, I made up this:
@echo off
setlocal EnableDelayedExpansion
REM change this to what you would do usually in your program:
for /l %%m in (1,1,5) do (
timeout /t 1
call:getNewTimestamp
echo !timestamp!
)
pause
goto:eof
:getNewTimestamp
for /f "delims== tokens=1*" %%g in ('wmic os get localdatetime /value') do (
if ".%%g"==".LocalDateTime" (
REM Change this to the usual way to get your timestamp:
set timestamp=%%h
)
)
Goto:eof
So whenever you need the current timestamp, you want to call :getNewTimeStamp
. This function will set !timestamp!
to the desired value. You can then use the value as usual in your main part of the program.
So my example has a loop that it goes through 5 times, each time waiting a second, then calling getNewTimestamp
and then echoing the value of !timestamp!
.
The term function
might be misleading here. It updates a script variable from within the same script, the goto:eof
at the end in combination together with the call <functionName>
- command, will result in updating the variable(s) accoring to the "functions script".