• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 219
  • Last Modified:

Using the time /t command

for /f "tokens=1,2 delims=:" %%a in ('time /t') do @echo scripttime=%%a%%b

Output is:
scripttime=0439 PM

What id like is:
scripttime=04_39_23_pm

So, id like to do the following:
1) factor in seconds as well eg. the 23 above,
2) remove the space before the PM and replace it with an underscore
3) convert all letters ot lowercase


Any help greatly appreciated.
0
Simon336697
Asked:
Simon336697
  • 2
  • 2
1 Solution
 
beanrodCommented:
Not sure if military time will suffice?

@echo Scripttime=%TIME:~0,2%_%TIME:~3,2%_%TIME:~6,2%

Output
Scripttime=20_54_49


0
 
Simon336697Author Commented:
Hi beanrod, thanks for that.

I extended yours to include whether the time was a single digit hour or second hour.
Thanks so much.


if "%TIME:~0,1%" == " " GOTO SINGLEDIGITHOUR
GOTO DOUBLEDIGITHOUR

:SINGLEDIGITHOUR
set Scripttime=0%TIME:~1,1%_%TIME:~3,2%_%TIME:~6,2%
GOTO END

:DOUBLEDIGITHOUR
set Scripttime=%TIME:~0,2%_%TIME:~3,2%_%TIME:~6,2%
GOTO END
0
 
Simon336697Author Commented:
Or another one:

@echo off

if defined Scripttime set Scripttime=

if "%TIME:~0,1%" == " " GOTO SINGLEDIGITHOUR
GOTO DOUBLEDIGITHOUR

:SINGLEDIGITHOUR
set Scripttime=0%TIME:~1,1%_%TIME:~3,2%_%TIME:~6,2%_AM
GOTO END

:DOUBLEDIGITHOUR
rem cater for 10AM or 11AM
if "%TIME:~0,2%" == "10" GOTO DOUBLEDIGITHOUR_10OR11AM
if "%TIME:~0,2%" == "11" GOTO DOUBLEDIGITHOUR_10OR11AM
GOTO DOUBLEDIGITHOUR_PM

:DOUBLEDIGITHOUR_10OR11AM
set Scripttime=%TIME:~0,2%_%TIME:~3,2%_%TIME:~6,2%_AM
GOTO END

:DOUBLEDIGITHOUR_PM
set Scripttime=%TIME:~0,2%_%TIME:~3,2%_%TIME:~6,2%_PM


:END
0
 
beanrodCommented:
Excellent glad you could manipulate it to your usage
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now