Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Date in YYYYMMDD format in windows batch file [duplicate]

I need to get the date in YYYYMMDD format in batch file.

I am doing this using :

set mydate=%date:~6,4%%date:~3,2%%date:~0,2% echo %mydate% 

I need it to be consistent across system, even on changing the time settings.

Please advise.

like image 778
user2061002 Avatar asked Feb 11 '13 10:02

user2061002


People also ask

Why is %% used in batch file?

%% in batch acted like \\ in bash. Where one would need to cancel the meaning of the previous percent-sign in a batch file; because variables in batch look like %var% . So because percent had a special meaning you needed to use %%var%% so a variable was still usable in a batch file.

What is %% K in batch file?

So %%k refers to the value of the 3rd token, which is what is returned.

What is %% g'in batch file?

%%parameter : A replaceable parameter: in a batch file use %%G (on the command line %G) FOR /F processing of a command consists of reading the output from the command one line at a time and then breaking the line up into individual items of data or 'tokens'.


1 Answers

If, after reading the other questions and viewing the links mentioned in the comment sections, you still can't figure it out, read on.

First of all, where you're going wrong is the offset.

It should look more like this...

set mydate=%date:~10,4%%date:~6,2%/%date:~4,2% echo %mydate% 

If the date was Tue 12/02/2013 then it would display it as 2013/02/12.

To remove the slashes, the code would look more like

set mydate=%date:~10,4%%date:~7,2%%date:~4,2% echo %mydate% 

which would output 20130212

And a hint for doing it in the future, if mydate equals something like %date:~10,4%%date:~7,2% or the like, you probably forgot a tilde (~).

like image 145
BDM Avatar answered Sep 27 '22 23:09

BDM