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 01:50

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%                                  
查看更多
聊天终结者
3楼-- · 2019-04-03 01:53

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
查看更多
Emotional °昔
4楼-- · 2019-04-03 01:54

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%
查看更多
We Are One
5楼-- · 2019-04-03 01:55

have you tried SET x=%date:~-10%, i know it works with windows 7.

查看更多
Melony?
6楼-- · 2019-04-03 01:57

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%
查看更多
啃猪蹄的小仙女
7楼-- · 2019-04-03 02:05

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.

查看更多
登录 后发表回答