Software >> OS >> Windows >> Command Line >> How to create a variable from date and time commands

(1) Method 1 for /f "tokens=2-4 delims=/ " %%a in ('Date /T') do set DateVar=%%c-%%a-%%b for /f "tokens=1-3 delims=: " %%a in ('TIME /T') do set TimeVar=%%a:%%b:%%c echo %DateVar% echo %TimeVar% To ensure 24 hour format for time, do a customisation of the Regional and Language Options from Control Panel (2) Method 2 @echo off for /f "tokens=1-2 delims=." %%a in ('echo %time%') do set TimeA=%%a& set hsec=%%b for /f "tokens=1-3 delims=:" %%a in ('echo %TimeA%') do set hh=%%a& set mm=%%b& set ss=%%c echo %hh%%mm%%ss%%hsec% (3) Method 3 for /f %%a in ('powershell -Command "Get-Date -format yyyyMMdd_HHmmss"') do set datetime=%%a ECHO %datetime%