Reputation: 12063
I need to get today date in Window *.bat
file. After it I would like to get day, month and year. How can I do this?
I can't use PowerShell
Upvotes: 51
Views: 202341
Reputation: 17
mixed solution as wmic is deprecated
REM CALL :get_fullstamp
REM then use one of thoses variables :
REM - %YY%
REM - %YYYY%
REM - %MM%
REM - %DD%
REM - %HH%
REM - %MIN%
REM - %SS%
REM - %datestamp%
REM - %timestamp%
REM - %fullstamp%
:get_fullstamp
WHERE wmic >nul 2>&1
IF %ERRORLEVEL% GTR 0 (
for /f "delims=" %%a in ('powershell -command "Get-Date -Format \"yyyyMMddHHmmss\""') do set "dt=%%a"
) else (
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do SET "dt=%%a"
)
SET "YY=%dt:~2,2%" & SET "YYYY=%dt:~0,4%" & SET "MM=%dt:~4,2%" & SET "DD=%dt:~6,2%" & SET "HH=%dt:~8,2%" & SET "MIN=%dt:~10,2%" & SET "SS=%dt:~12,2%"
SET "datestamp=%YYYY%%MM%%DD%"
SET "timestamp=%HH%%MIN%%SS%"
SET "fullstamp=%datestamp%_%timestamp%"
GOTO :EOF
Upvotes: -1
Reputation: 131
To revert date like this "24.12.2023" to "2023-12-24" and create directory:
FOR /f "tokens=1-3 delims=. " %%a IN ("%date%") DO SET dt=%%c-%%b-%%a
md %dt%
Upvotes: 0
Reputation: 830
You get and format like this
for /f "tokens=1-4 delims=/ " %%i in ("%date%") do (
set dow=%%i
set month=%%j
set day=%%k
set year=%%l
)
set datestr=%month%_%day%_%year%
echo datestr is %datestr%
Note: Above only works on US locale. It assumes the output of echo %date%
looks like this: Thu 02/13/21
. If you have different Windows locale settings, you will need to modify the script based on your configuration.
Upvotes: 43
Reputation: 33
This will give you the date in this format mmddyyyy saved into %today%, assuming your regional settings have you in the US locale.
FOR /f "tokens=2-4 delims=/ " %%a IN ("%date%") DO SET today=%%a%%b%%c
In my case I was looking to create a folder with todays date, like this...
mkdir C:\Users\LKrell\Documents\%today%
Upvotes: 1
Reputation: 319
Locale-independent one liner to get any date format you like. I use it to generate archive names. Back quote (`) option is needed because PowerShell command line is using single quotes (').
:: Get date in 'yyyyMMdd_HHmm' format to use with file name.
FOR /f "usebackq" %%i IN (`PowerShell ^(Get-Date^).ToString^('yyyyMMdd_HHmm'^)`) DO SET DTime=%%i
:: Get yesterday date in 'yyyy-MM-dd' format.
FOR /f "usebackq" %%i IN (`PowerShell ^(Get-Date^).AddDays^(-1^).ToString^('yyyy-MM-dd'^)`) DO SET DTime=%%i
:: Show file name with the date.
echo Archive.%DTime%.zip
Upvotes: 13
Reputation: 41297
This will give you DD MM YYYY YY HH Min Sec
variables and works on any Windows machine from XP Pro and later.
@echo off
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"
set "datestamp=%YYYY%%MM%%DD%" & set "timestamp=%HH%%Min%%Sec%"
set "fullstamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%"
echo datestamp: "%datestamp%"
echo timestamp: "%timestamp%"
echo fullstamp: "%fullstamp%"
pause
Upvotes: 100
Reputation: 1714
%date%
will give you the date.
%time%
will give you the time.
The date
and time /t
commands may give you more detail.
Upvotes: 33