I want to set a Windows environment variable from the results of TIME /t.

In Windows, the %time% variable yields one format (13:17:52.79), whereas TIME /t has another (01:17 PM).

I've tried sending it to a text file and trying to append "SET xxx=" but there is always a line feed that I cannot seem to eliminate.

For example, I can "ECHO Set xxx= >> TTT.txt" and "TIME /t >> TTT.txt" but the results are on two separate lines. The same is true when I make two files and try to COPY /b. I have also experimented with similar methods using WMIC.

Any ideas?


In cmd.exe, for /f can be used to read command output:

for /f %%a in ('time /t') do set xxx=%%a

See for /? for more details.

| improve this answer | |
  • Thanks! Almost there! It misses the AM/PM. I assume that's because there is a space in the output between the numbers and the AM/PM. I read the /? for the "FOR" command and tried a few variations but no luck yet. – Southern Builder Aug 1 '18 at 21:01
  • Ah, use "tokens=*" to catch multiple words. – user1686 Aug 1 '18 at 21:27
  • That does it! Thank you so much! I've been fighting this for so long! – Southern Builder Aug 1 '18 at 21:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.