This was inspired by another post on Arqade, on how to backup some files using a batch script.
The following block creates a variable with the current date/time string:
REM -- Get the current date/time stamp
set DS=%date%
set TS=%time: =0%
set mm=%DS:~4,2%
set dd=%DS:~7,2%
set yyyy=%DS:~10,4%
set hh=%TS:~0,2%
set min=%TS:~3,2%
set ss=%TS:~6,2%
set ms=%TS:~9,2%
set DT_STAMP=%yyyy%-%mm%-%dd%_%hh%.%min%.%ss%
As a script writer, it is often convenient to consolidate these down to a single line. However, in this case, condensing this to a single line is extremely difficult, if not impossible.
There does not seem to be a way to put multiple set
commands on a single line. Separating commands with &
or &&
works fine, but commands on the right cannot be dependent on variables that were set earlier on the same line.
Also, notice how the time
variable must have spaces
replaced with zeros 0
. There does not seem to be a way to do both a string replacement and get a sub-string on the same line.
Is there any way to get this down to one line? The closest I can get is two lines:
set DS=%date% && set TS=%time: =0%
set DT_STAMP=%DS:~10,4%-%DS:~4,2%-%DS:~7,2%_%TS:~0,2%.%TS:~3,2%.%TS:~6,2%
Update
This has gathered some good answers; allow me to clarify what an accepted answer must have:
- An ugly, but re-usable, single line solution is OK (the pretty version is already provided above)
- Standard shell commands only (PowerShell and similar must be avoided; if I wanted those, I'd just do the whole thing in PowerShell)
- Ability to format the date in any logical format (should be able to do anything the pretty version above can do, not just support ISO formatted dates using shorthand notation)
- It is OK to ignore localization settings (really, just this one time!)
- Must be on a single line! (for instance, delayed expansion is handy, but can have nasty side-effects, is not re-usable in every context, and usually requires multiple lines)