How to set date from cmd after retrieving it

2019-04-03 01:36发布

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

8条回答
闹够了就滚
2楼-- · 2019-04-03 02:11

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.

查看更多
太酷不给撩
3楼-- · 2019-04-03 02:15

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.

查看更多
登录 后发表回答