我有以下代码
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
所以,我怎样才能在格式DD / MM / YY日期
我有以下代码
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
所以,我怎样才能在格式DD / MM / YY日期
您可以通过使用获得DD / MM / YY日期格式wmic
命令。 这个命令可以让你获得当前日期而不会受到区域设置。
@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
你可以把它保存为date.bat
并通过执行在命令提示符下运行这个批处理文件:
C:\>date.bat
21/01/13
Press any key to continue...
希望这可以帮助。
另外两个方面是不依赖于时间设置(均摘自:如何从本地化获取数据/时间无关: )。而双方也得到了一周的一天,没有人需要管理员权限!:
1. MAKECAB -将工作在每一个Windows系统(快,但创建一个小的临时文件)(该foxidrive脚本):
@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 -这不是本机命令的Windows XP和2003夺冠,但可以从微软网站下载 。但被内置在一切从Vista及以上:
@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
还有三个使用其他Windows脚本languages.They方式会给你更多的灵活性,例如,你可以得到一年的星期,时间以毫秒为单位等。
3. JSCRIPT /批混合(需要保存为.bat
).Jscript可对每个系统形式NT及以上,如Windows脚本宿主的一部分( 虽然可以通过它的注册表中的一个罕见的情况下被禁用 ):
@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杂交体( 是否有可能嵌入和不使用临时文件的批处理文件中的VBScript执行? )相同的情况下,JScript中,但杂交是不那么完美:
: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 -可以每天拥有.NET计算机上安装-从Microsoft下载( V1 , V2 , V3 (仅适用于WIN7及以上)),默认的一切形式的Win7 /的Win2008及以上的安装。
C:\>powershell get-date -format "{dd-MMM-yyyy HH:mm}"
6. 自编jscript.net/batch(从来没有见过一个Windows机器没有.NET所以我觉得这是一个漂亮的便携式):
@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这不能拿到年终,日的week.It的速度比较慢,还创建一个临时文件是基于logman在其日志放files.Will工作,一切从XP和above.Probably将是永远的时间戳使用的人 - 包括我在内 - 却是多了一个办法...
@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
有关获取最新的功能的更多信息 。
这是我的系统上进行测试,并将其设置的日期要求的格式。 在没有错误消息“没有足够的所需的权限或所需的特权不保留由客户端”的情况下,尝试运行该脚本为管理员。 这应该工作得很好,那么。
@echo off
set x=%DATE:~0,2%/%DATE:~3,2%/%DATE:~6,4%
echo %x%
set date=%x%
echo %date%
你尝试过集X =%日期:〜-10%,我知道它的工作原理与Windows 7。
你的任务set x=%date /T %
是错误的。 百分号扩展变量,它们不提供命令替换功能。
窗户已经提供了一个自动的环境变量%DATE%
当前日期(也可变%TIME%
当前时间),所以你可以简单地这样做:
set x=%DATE%
date 16/12/2012
date 15/12/2012
rem stuff
date %x%
pause
戴尔的回答对我帮助很大。 我修改了他的解决方案,以获得在格式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%
我知道这是旧的文章,但我已经找到了一种方法来做到这一点不WMIC:
起初,我发现了一种答案,工作与的:
你可以试试这个! 这应该在Windows机器上运行。
为/ F “有usebackq令牌= 1,2,3 delims = - ” %% I IN(
echo %date%
)做回声“%% I” “%% J” “%% K” https://stackoverflow.com /一个/3735825分之14820126
然后,我曾与这一点,这样做是一个:
for /F "usebackq tokens=1,2,3,4 delims=/, " %%I IN (`echo %date%`) do ECHO %%L-%%K-%%J
它删除的空间,并从%日期%指令的分隔符,让你使用任何类型的日期作为变量的一部分; 例如,如果我要设置日期为年月日的代码应该是这样的:
for /F "usebackq tokens=1,2,3,4 delims=/, " %%I IN (`echo %date%`) do ECHO %%L%%K%%J
如果你需要单独的变量,你需要使用它们,只要你想,那么你可以用下面的变量发挥:
%%I = Day of Week
%%J = Day
%%K = Month
%%L = Year
我希望它帮助任何人。
下面的代码对我的作品:
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%
就我而言,我需要在格式YYYY-MM-DD的日期,而不是在2019年五月-09。 所以,我从修改与补充驾驶室npocmaka通用决定:
@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-year=%%e"
set "current-month=%%b"
set "current-day=%%c"
set "current-time=%%d"
set "weekday=%%a"
)
del ~.*
popd
CALL :CASE_%current-month%
IF ERRORLEVEL 1 CALL :DEFAULT_CASE
ECHO Done.
GOTO NEXT
:CASE_Jan
set "current-month=01"
GOTO END_CASE
:CASE_Feb
set "current-month=02"
GOTO END_CASE
:CASE_Mar
set "current-month=03"
GOTO END_CASE
:CASE_Apr
set "current-month=04"
GOTO END_CASE
:CASE_May
set "current-month=05"
GOTO END_CASE
:CASE_Jun
set "current-month=06"
GOTO END_CASE
:CASE_Jul
set "current-month=07"
GOTO END_CASE
:CASE_Aug
set "current-month=08"
GOTO END_CASE
:CASE_Sep
set "current-month=09"
GOTO END_CASE
:CASE_Oct
set "current-month=10"
GOTO END_CASE
:CASE_Nov
set "current-month=11"
GOTO END_CASE
:CASE_Dec
set "current-month=12"
GOTO END_CASE
:DEFAULT_CASE
set "current-month=xx"
echo "Month is not in the English language! Fix it!"
GOTO END_CASE
:END_CASE
VER > NUL # reset ERRORLEVEL
GOTO :EOF # return from CALL
:NEXT
echo %weekday% %current-year%-%current-month%-%current-day% %current-time%
pause
经测试在Windows XP SP3和Server 2008 R2 SP1。