I have the following code
set x=%date /T %
date 16/12/2012
date 15/12/2012
some stuff goes here
echo set your date
date %x% <--- getting error in that line.
pause
So how can i get the date in the format dd/mm/yy
I have the following code
set x=%date /T %
date 16/12/2012
date 15/12/2012
some stuff goes here
echo set your date
date %x% <--- getting error in that line.
pause
So how can i get the date in the format dd/mm/yy
You can get the date format of dd/mm/yy by using wmic
command. This command allows you to get current date without getting affected by regional settings.
@echo off
SETLOCAL EnableDelayedExpansion
for /f "skip=1 tokens=1-6 delims= " %%a in ('wmic path Win32_LocalTime Get Day^,Hour^,Minute^,Month^,Second^,Year /Format:table') do (
IF NOT "%%~f"=="" (
set /a FormattedDate=10000 * %%f + 100 * %%d + %%a
set FormattedDate=!FormattedDate:~-2,2!/!FormattedDate:~-4,2!/!FormattedDate:~-6,2!
)
)
echo %FormattedDate%
PAUSE
You can save it as date.bat
and run this batch file by executing the following in command prompt:
C:\>date.bat
21/01/13
Press any key to continue...
Hope this helps.
Two more ways that do not depend on the time settings (both taken from :How get data/time independent from localization:).And both also get the day of the week and none of them requires admin permissions!:
1.MAKECAB - will work on EVERY windows system (fast but creates a small temp file ) (the foxidrive script):
@echo off
pushd "%temp%"
makecab /D RptFileName=~.rpt /D InfFileName=~.inf /f nul >nul
for /f "tokens=3-7" %%a in ('find /i "makecab"^<~.rpt') do (
set "current-date=%%e-%%b-%%c"
set "current-time=%%d"
set "weekday=%%a"
)
del ~.*
popd
echo %weekday% %current-date% %current-time%
pause
2. ROBOCOPY - it's not native command for windows xp and win 2003 but can be downloaded from microsoft site .But is built-in in everything from Vista and above:
@echo off
setlocal
for /f "skip=8 tokens=2,3,4,5,6,7,8 delims=: " %%D in ('robocopy /l * \ \ /ns /nc /ndl /nfl /np /njh /XF * /XD *') do (
set "dow=%%D"
set "month=%%E"
set "day=%%F"
set "HH=%%G"
set "MM=%%H"
set "SS=%%I"
set "year=%%J"
)
echo Day of the week: %dow%
echo Day of the month : %day%
echo Month : %month%
echo hour : %HH%
echo minutes : %MM%
echo seconds : %SS%
echo year : %year%
endlocal
And three more ways that uses other windows script languages.They will give you more flexibility e.g. you can get week of the year, time in milliseconds and so on.
3.JSCRIPT/BATCH hybrid (need to be saved as .bat
).Jscript is available on every system form NT and above , as a part of windows script host (though can be disabled through the registry it's a rare case):
@if (@X)==(@Y) @end /* ---Harmless hybrid line that begins a JScript comment
@echo off
cscript //E:JScript //nologo "%~f0"
exit /b 0
*------------------------------------------------------------------------------*/
function GetCurrentDate() {
// Today date time which will used to set as default date.
var todayDate = new Date();
todayDate = todayDate.getFullYear() + "-" +
("0" + (todayDate.getMonth() + 1)).slice(-2) + "-" +
("0" + todayDate.getDate()).slice(-2) + " " + ("0" + todayDate.getHours()).slice(-2) + ":" +
("0" + todayDate.getMinutes()).slice(-2);
return todayDate;
}
WScript.Echo(GetCurrentDate());
4.VSCRIPT/BATCH hybrid (Is it possible to embed and execute VBScript within a batch file without using a temporary file?) same case as jscript , but hybridization is not so perfect:
:sub echo(str) :end sub
echo off
'>nul 2>&1|| copy /Y %windir%\System32\doskey.exe %windir%\System32\'.exe >nul
'& echo current date:
'& cscript /nologo /E:vbscript "%~f0"
'& exit /b
'0 = vbGeneralDate - Default. Returns date: mm/dd/yy and time if specified: hh:mm:ss PM/AM.
'1 = vbLongDate - Returns date: weekday, monthname, year
'2 = vbShortDate - Returns date: mm/dd/yy
'3 = vbLongTime - Returns time: hh:mm:ss PM/AM
'4 = vbShortTime - Return time: hh:mm
WScript.echo Replace(FormatDateTime(Date,1),", ","-")
5.POWERSHELL - can be installed on every machine that has .net - download from Microsoft (v1 , v2 , v3 (only for win7 and above)).Installed by default on everything form Win7/Win2008 and above :
C:\>powershell get-date -format "{dd-MMM-yyyy HH:mm}"
6.Self-compiled jscript.net/batch (never seen a windows machine without .net so I think this is a pretty portable):
@if (@X)==(@Y) @end /****** silent line that start jscript comment ******
@echo off
::::::::::::::::::::::::::::::::::::
::: compile the script ::::
::::::::::::::::::::::::::::::::::::
setlocal
if exist "%~n0.exe" goto :skip_compilation
set "frm=%SystemRoot%\Microsoft.NET\Framework\"
:: searching the latest installed .net framework
for /f "tokens=* delims=" %%v in ('dir /b /s /a:d /o:-n "%SystemRoot%\Microsoft.NET\Framework\v*"') do (
if exist "%%v\jsc.exe" (
rem :: the javascript.net compiler
set "jsc=%%~dpsnfxv\jsc.exe"
goto :break_loop
)
)
echo jsc.exe not found && exit /b 0
:break_loop
call %jsc% /nologo /out:"%~n0.exe" "%~dpsfnx0"
::::::::::::::::::::::::::::::::::::
::: end of compilation ::::
::::::::::::::::::::::::::::::::::::
:skip_compilation
"%~n0.exe"
exit /b 0
****** end of jscript comment ******/
import System;
import System.IO;
var dt=DateTime.Now;
Console.WriteLine(dt.ToString("yyyy-MM-dd hh:mm:ss"));
7.Logman This cannot get the year and day of the week.It's comparatively slow , also creates a temp file and is based on the time stamps that logman puts on its log files.Will work everything from XP and above.Probably will be never used by anybody - including me - but is one more way...
@echo off
setlocal
del /q /f %temp%\timestampfile_*
Logman.exe stop ts-CPU 1>nul 2>&1
Logman.exe delete ts-CPU 1>nul 2>&1
Logman.exe create counter ts-CPU -sc 2 -v mmddhhmm -max 250 -c "\Processor(_Total)\%% Processor Time" -o %temp%\timestampfile_ >nul
Logman.exe start ts-CPU 1>nul 2>&1
Logman.exe stop ts-CPU >nul 2>&1
Logman.exe delete ts-CPU >nul 2>&1
for /f "tokens=2 delims=_." %%t in ('dir /b %temp%\timestampfile_*^&del /q/f %temp%\timestampfile_*') do set timestamp=%%t
echo %timestamp%
echo MM: %timestamp:~0,2%
echo dd: %timestamp:~2,2%
echo hh: %timestamp:~4,2%
echo mm: %timestamp:~6,2%
endlocal
exit /b 0
more information about get-date function.
This is tested on my system and it sets the date to required format. In case of error message of not "not enough required privileges or A required privilege is not held by the client" try running the script as Administrator. This should work fine then.
@echo off
set x=%DATE:~0,2%/%DATE:~3,2%/%DATE:~6,4%
echo %x%
set date=%x%
echo %date%
have you tried SET x=%date:~-10%, i know it works with windows 7.
Your assignment set x=%date /T %
is wrong. The percent signs expand variables, they don't provide command substitution functionality.
Windows already provides an automatic environment variable %DATE%
with the current date (also a variable %TIME%
with the current time), so you can simply do this:
set x=%DATE%
date 16/12/2012
date 15/12/2012
rem stuff
date %x%
pause
Dale's answer helped me a lot. I modified his solution to get the time in format hhmmdd:
@echo off
SETLOCAL EnableDelayedExpansion
rem Fill variables: %%a=Day %%b=Hour %%c=Minute %%d=Month %%e=Second %%f=Year
for /f "skip=1 tokens=1-6 delims= " %%a in ('wmic path Win32_LocalTime Get Day^,Hour^,Minute^,Month^,Second^,Year /Format:table') do (
IF NOT "%%~f"=="" (
rem Adding leading zeros:
set /a FormattedTime=1000000 + 10000 * %%b + 100 * %%c + %%e
rem reading from the right to left:
set FormattedTime=!FormattedTime:~-6,2!!FormattedTime:~-4,2!!FormattedTime:~-2,2!
)
)
echo %FormattedTime%
I know this is an old post, but I've found a way to do it without wmic:
At first, I've found a kind of answer and work with that:
You can try this ! This should work on windows machines.
for /F "usebackq tokens=1,2,3 delims=-" %%I IN (
echo %date%
) do echo "%%I" "%%J" "%%K" https://stackoverflow.com/a/14820126/3735825
Then I worked with this and did this one:
for /F "usebackq tokens=1,2,3,4 delims=/, " %%I IN (`echo %date%`) do ECHO %%L-%%K-%%J
It deletes the spaces and the separators from the %date% command and let you use any kind of part of the date as a variable; for example if I want to set the date as YYYYMMDD The code should be like this:
for /F "usebackq tokens=1,2,3,4 delims=/, " %%I IN (`echo %date%`) do ECHO %%L%%K%%J
If do you need the variables alone and you need to use them as you want, then you can play with the following variables:
%%I = Day of Week
%%J = Day
%%K = Month
%%L = Year
I hope it help anyone.
The following code works for me:
set DATE_NOW=!date:~6,4!!date:~0,2!!date:~3,2!
set TIME_NOW=!time:~0,2!!time:~3,2!!time:~6,2!
echo %DATE_NOW%
echo %TIME_NOW%