batch-file - 批处理文件 - 在检索之后,如何从cmd设置日期

我有以下代码


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格式获得日期呢

时间:

你可以使用wmic命令获取dd/mm/yy的日期格式,此命令允许你获取当前日期而不受区域设置影响。


 @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...



希望这个能帮到你。

在我的系统上测试它,它将日期设置为所需格式,如果不是"权限不足"或所需权限不是由客户机持有的,请尝试以管理员身份运行脚本,这应该能正常工作。


@echo off


set x=%DATE:~0,2%/%DATE:~3,2%/%DATE:~6,4%


echo %x%


set date=%x%


echo %date%



Dale的回答对我很有帮助。我修改了他的解决方案以获取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%



下面的代码适用于我:


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-May-09,因此,我使用make cab从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和服务器2008 R2上测试过了。

...