Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get date in BAT file

I need to get today date in Window *.bat file. After it I would like to get day, month and year. How can I do this?
I can't use PowerShell

like image 402
Jacek Avatar asked Oct 02 '13 06:10

Jacek


People also ask

How do I echo date in CMD?

On a Microsoft Windows system, you can obtain the current date using the date /t command (the /t option prevents the command from prompting for a change to the the date) or by using echo %date% to display the contents of the date environment variable.

Which command is used to display date in dd mm yyyy format in MS DOS?

In modern versions of Windows, the date command accepts a four-digit date, e.g., MM-DD-YYYY. With the /t switch, the date command displays the system date, without prompting for a new one.

What does date do in CMD?

The date command displays or sets the system date. It is most commonly used to print the date and time in different formats and calculate future and past dates.


1 Answers

This will give you DD MM YYYY YY HH Min Sec variables and works on any Windows machine from XP Pro and later.

@echo off for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a" set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%" set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"  set "datestamp=%YYYY%%MM%%DD%" & set "timestamp=%HH%%Min%%Sec%" set "fullstamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%" echo datestamp: "%datestamp%" echo timestamp: "%timestamp%" echo fullstamp: "%fullstamp%" pause 
like image 74
foxidrive Avatar answered Oct 04 '22 11:10

foxidrive