Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the day month and year from a Windows cmd.exe script?

How do I get the current day month and year from inside a Windows cmd script? I need to get each value into a separate variable.

like image 798
Mossen Avatar asked Aug 12 '10 22:08

Mossen


People also ask

How do I timestamp in CMD?

You can use $T for time and $D for date, as well as several other expansions. See also: https://ss64.com/nt/prompt.html. https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/prompt.

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.

How do I get current date time on the Windows command line?

cmd - Script to get current time. GMT. cmd - Current time in GMT (World Time). Equivalent PowerShell: Get-Date - Get current date and time.


1 Answers

To get the year, month, and day you can use the %date% environment variable and the :~ operator. %date% expands to something like Thu 08/12/2010 and :~ allows you to pick up specific characters out of a variable:

set year=%date:~10,4% set month=%date:~4,2% set day=%date:~7,2% set filename=%year%_%month%_%day% 

Use %time% in similar fashion to get what you need from the current time.

set /? will give you more information on using special operators with variables.

like image 93
poke Avatar answered Oct 06 '22 03:10

poke