Skip navigation

JSI Tip 9450. How can I return the current date and time in a string like YYYYMMDD_HHMNSS?


I have scripted FnDT.bat to return the current date and time in a string that is formatted like YYYYMMDD_HHMNSS.

The syntax for using FnDT.bat is:

call FnDT DateTime

Where DateTime is a call directed environment variable that will contain the current date and time in a YYYYMMDD_HHMNSS formatted string.

FnDT.bat contains:

@echo off
if \{%1\}==\{\} @echo Syntax: call FnDT DateTtime&goto :EOF
setlocal
if exist "%TEMP%\FnDT.vbs" goto doit
@echo dDate = Now>"%TEMP%\FnDT.vbs"
@echo sDateTime = Year^(dDate^) ^& Right^(100 + Month^(dDate^), 2^) _>>"%TEMP%\FnDT.vbs"
@echo.          ^& ^Right^(100 + Day^(dDate^), 2^) ^& "_" ^& Right^(100 + Hour^(dDate^), 2^) _>>"%TEMP%\FnDT.vbs"
@echo.          ^& Right^(100 + Minute^(dDate^), 2^) ^& Right^(100 + Second^(dDate^), 2^)>>"%TEMP%\FnDT.vbs"
@echo WScript.Echo sDateTime>>"%TEMP%\FnDT.vbs"
:doit
for /f "Tokens=*" %%a in ('cscript.exe //nologo "%TEMP%\FnDT.vbs"') do (
 set DT=%%a
)
endlocal&set %1=%DT%



Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish