2

How can I create a variable in windows command line (CMD) that uses the following format:

MON-DAY-YEAR_TIME-AM (or PM)

set mon=%DATE:~4,2%
set day=%DATE:~7,2%
set yr=%DATE:~10,4%

> echo %mon%-%day%-%yr%
02-12-2018    # Good so far now I need to add time

Trying to set the output of TIME /T (10:54 AM) to a variable so I can use it with the date above. How can I do this?

> TIME /T
10:54 AM

Not what im looking for:

>set mytime=TIME /T

>echo %mytime%
TIME /T
jes516
  • 542
  • 1
  • 13
  • 30
  • 2
    [`%date%` is not a reliable way to get date](https://stackoverflow.com/q/15378719/995714). And to get time just use `%time%` – phuclv Feb 12 '18 at 16:01
  • to read stdout use [`for /f`](https://stackoverflow.com/q/6359820/995714) – phuclv Feb 12 '18 at 16:10

2 Answers2

2

Use WMIC OS GET LOCALDATETIME instead:

FOR /F "skip=1" %%A IN ('WMIC OS GET LOCALDATETIME') DO (SET "t=%%A" & GOTO break_1)
:break_1

SET "m=%t:~10,2%" & SET "h=%t:~8,2%" & SET "d=%t:~6,2%" & SET "z=%t:~4,2%" & SET "y=%t:~0,4%"
ECHO %z%-%d%-%y%_%h%:%m%

Output: 02-12-2018_17:27

--------------------------------------------------------------------------------------------------------

If you don't like the 24h format for whatsoever reason, use this:

SETLOCAL EnableDelayedExpansion

FOR /F "skip=1" %%A IN ('WMIC OS GET LOCALDATETIME') DO (SET "t=%%A" & GOTO break_1)
:break_1
SET "m=%t:~10,2%" & SET "h=%t:~8,2%" & SET "d=%t:~6,2%" & SET "z=%t:~4,2%" & SET "y=%t:~0,4%"
IF !h! GTR 11 (SET /A "h-=12" & SET "ap=P" & IF "!h!"=="0" (SET "h=00") ELSE (IF !h! LEQ 9 (SET "h=0!h!"))) ELSE (SET "ap=A")

ECHO %z%-%d%-%y%_%h%:%m%-%ap%M

Output: 02-12-2018_05:27-PM


And if you don't want an additional 0 in front of single digit hours, use this:

FOR /F "skip=1" %%A IN ('WMIC OS GET LOCALDATETIME') DO (SET "t=%%A" & GOTO break_1)
:break_1
SET "m=%t:~10,2%" & SET "h=%t:~8,2%" & SET "d=%t:~6,2%" & SET "z=%t:~4,2%" & SET "y=%t:~0,4%"
IF "%h%"=="00" (SET "h=0") ELSE (IF %h% LEQ 9 SET "h=%h:0=%")
IF %h% GTR 11 (SET /A "h-=12" & SET "ap=P") ELSE (SET "ap=A")

ECHO %z%-%d%-%y%_%h%:%m%-%ap%M

Output: 02-12-2018_5:27-PM

FatalBulletHit
  • 762
  • 6
  • 22
  • Is it possible to get the AM/PM? – jes516 Feb 12 '18 at 16:32
  • @jes516 If you refresh the page/post... ^^ – FatalBulletHit Feb 12 '18 at 16:33
  • @jes516 Had a small mistake in there, `12 AM` was `12 PM` and the other way around. Fixed it just now! :) – FatalBulletHit Feb 12 '18 at 17:24
  • Both sets of your code output the leading zero for the hour – Squashman Feb 12 '18 at 18:15
  • @Squashman Nope, the result of `SET /A "17-12"` is `5` and not `05`. – FatalBulletHit Feb 12 '18 at 18:16
  • This is what I get for 9:15 AM `02-12-2018_09:15-AM 02-12-2018_09:15-AM` – Squashman Feb 12 '18 at 18:16
  • With AM yes, as I don't need to makes use of `SET /A` (that's why the example has PM in it). – FatalBulletHit Feb 12 '18 at 18:18
  • And 12:15 pm outputs this for both of them. `02-12-2018_00:15-PM` `02-12-2018_0:15-PM` – Squashman Feb 12 '18 at 18:21
  • @Squashman yep, it's ripped out of a bigger script of mine, forgot about the exclusions, will add them now! :) – FatalBulletHit Feb 12 '18 at 18:22
  • The bottom version (the one that removes the leading zeros) no longer runs because it's missing the ending quote at the end of the set command on the fourth line. `(IF %h% LEQ 9 SET "h=%h:0=%)` should be `(IF %h% LEQ 9 SET "h=%h:0=%")` – Brent Rittenhouse Mar 03 '19 at 21:47
  • I edited the final answer to fix the missing quote, and also use set/endlocal so that the calling environment doesn't get polluted with the temporary variables (minus the now resulting 'datetime'). This was mostly to get around the fact that I couldn't edit and add just one character =/ Feel free to change it however you want, and thanks for the script in general! – Brent Rittenhouse Mar 03 '19 at 22:03
  • @BrentRittenhouse Thanks for your comment and edit suggestion; I fixed the missing quotation mark, but I don't think the mention of `SETLOCAL` and `ENDLOCAL` is needed in this example as it is meant to be a code snippet only and it's not essential for it to work. :) – FatalBulletHit Mar 12 '19 at 18:23
  • @FatalBulletHit, agreed. Like I said, it was mostly to get around the fact that I couldn't do such a simple edit. Your code was fantastic as-is. If anyone wants to see a 'setlocal' version they can always go back through the edits anyway, right? Seriously, thank you very much for the awesome snippet! It's something I've used on multiple occasions. edit: Looks like you removed the history. Doesn't matter. People can modify it as needed. Have a great day! – Brent Rittenhouse Mar 25 '19 at 06:03
  • @BrentRittenhouse I didn't remove the history, propably was automated. Also, glad I could help, have a nice one, too! ;) – FatalBulletHit Apr 06 '19 at 10:58
0

Another reliable and locale independent way would be to use PowerShell.

FOR /F %%t IN ('powershell -NoProfile -Command "Get-Date -UFormat '%I:%M %p'"') DO (
    SET "mytime=%%t"
)

ECHO %mytime%
lit
  • 14,456
  • 10
  • 65
  • 119
  • trying to stay away from powershell for this particular script. for whatever reason powershell does not play nice when copying files that are actively being written to. even when calling robocopy from within powershell sometimes just hangs which is why im moving them over to batch scripts. i have a powershell script going in after and zipping the files and removing the originals. – jes516 Feb 12 '18 at 22:07
  • 1
    I do not see how using a cmd.exe .bat script will do anything better when copying files that are still being written. Good luck with your choice. – lit Feb 12 '18 at 22:29
  • I didnt say it was better. Is it common practice to call cmd commands from within powershell? That powershell script i have does not complete for me through task scheduler. Manually it runs. Exploring all options... – jes516 Feb 13 '18 at 01:36
  • When necessary, it is fine to call other executables from PowerShell. See the `&` invocation operator. – lit Feb 13 '18 at 01:52