I agree with Compos and Mofis comments, but IMO there are a lot of outdated variants proposed here on SO with:
- locale/user settings dependent
%date%
and %time%
variables or
- the clumsy to parse
wmic
variant with it's cr/cr/lf sequences.
- J-/vbscript solutions require either a temp file or a hybrid version difficult to understand for newbies.
Using/parsing PowerShell comes at a small delay, but as it is usually used only once in a script that shouldn't matter.
You may insert an day offset to get different dates.
@Echo off
For /f %%Y in (
'powershell -NoP -C "(get-date).AddDays(-1).ToString(\"yyyy-MM-dd\")"'
) Do Set Yesterday=%%Y
Echo Yesterday = %Yesterday%
For /F %%D in (
'powershell -NoP -C "(get-date).AddDays(0).ToString(\"MM\/dd\/yyyy\")"'
) Do Set "DT=%%D"
Echo Today mdy = %DT%
For /f %%T in (
'powershell -NoP -C "(get-date).AddDays(+1).ToString(\"yyyy\/MM\/dd\")"'
) Do Set Tomorrow=%%T
Echo Tomorrow = %Tomorrow%
For /f %%o in (
'powershell -NoP -C "(get-date).AddDays(0).ToString(\"o\")"'
) Do Set ISO8601=%%o
Echo ISO8601 = %ISO8601%
For /f "delims=" %%U in (
'powershell -NoP -C "(get-date).AddDays(0).ToString(\"U\")"'
) Do Set UTC=%%U
Echo UTC = %UTC%
For /f "delims=" %%F in (
'powershell -NoP -C "(get-date).AddDays(0).ToString(\"F\")"'
) Do Set FullDT=%%F
Echo FullDT = %FullDT%
Output on my German locale system, day-/month names come in your locale.
Yesterday = 2017-08-17
Today mdy = 08/18/2017
Tomorrow = 2017/08/19
ISO8601 = 2017-08-18T17:06:46.1050969+02:00
UTC = Freitag, 18. August 2017 15:06:46
FullDT = Freitag, 18. August 2017 17:06:46
See this link for more format specifiers