Skip navigation

JSI Tip 9905. How can I generate a 14 digit year, month, day, hour, minute, seconds string for the current date / time?


Using standard commands, I have scripted NowStr.bat to generate a 14 digit year, month, day, hour, minute, second string for the current date / time, regardless of your date / time format.

The syntax for using NowStr.bat is:

\[call\] NowStr YMDHMS

Where YMDHMS is a call directed environment variable that will contain the four digit year, 2 digit month, 2 digit day, 2 digit hour, 2 digit minute, and 2 digit second for the current date / time.

NowStr.bat contains:

@echo off
if \{%1\}==\{\} @echo Syntax: NowStr YMDHMS&goto :EOF
if exist "%TEMP%\NowStr.VBS" goto setStr
@echo.YMDHMS = Right("20"^&Year(Now()),4) ^& _>"%TEMP%\NowStr.VBS"
@echo.         Right("0"^&Month(Now()),2) ^& _>>"%TEMP%\NowStr.VBS"
@echo.         Right("0"^&Day(Now()),2) ^& _>>"%TEMP%\NowStr.VBS"
@echo.         Right("0"^&Hour(Now()),2) ^& _>>"%TEMP%\NowStr.VBS"
@echo.         Right("0"^&Minute(Now()),2) ^& _>>"%TEMP%\NowStr.VBS"
@echo.         Right("0"^&Second(Now()),2)>>"%TEMP%\NowStr.VBS"
@echo.Wscript.Echo YMDHMS>>"%TEMP%\NowStr.VBS"
:setStr
for /f "Tokens=*" %%n in ('cscript //nologo "%TEMP%\NowStr.VBS"') do (
 set %1=%%n
)



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