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.
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.
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.
cmd - Script to get current time. GMT. cmd - Current time in GMT (World Time). Equivalent PowerShell: Get-Date - Get current date and time.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With