Page 1 of 2
display time in 12 hr
Posted: 14 Jul 2011 01:40
by kumar_kondapalli
HI ,
Can we display time in 12 hr format using batch file
thanks,
san
Re: display time in 12 hr
Posted: 14 Jul 2011 09:33
by nitt
kumar_kondapalli wrote:HI ,
Can we display time in 12 hr format using batch file
thanks,
san
What do you mean? When I use "echo %time%" it already is in the 12 hour format. :/
If you want to remove the part of time where it says the seconds, just do something like this:
Code: Select all
@echo off
for /f "tokens=1 delims=:" %%a in ('echo %time%') do (set t=%%a)
for /f "tokens=2 delims=:" %%a in ('echo %time%') do (set t=%t%:%%a)
echo %t%
pause
Re: display time in 12 hr
Posted: 14 Jul 2011 10:03
by kumar_kondapalli
hi nitt,
Thanks for your reply. Actually i had a bat file where it appends the date and time for a pdf file.But it displays in 24 hr format. I want to display it in 12 hr format . Can you please modify the below code and make it working?I am very new to this bat code , Below is the code ...
@echo on
@For /F "tokens=1,2,3,4 delims=/ " %%A in ('Date /t') do @(
Set DayW=%%A
Set Day=%%B
Set Month=%%C
Set Year=%%D
Set All=%%A-%%B-%%C-%%D
)
@For /F "tokens=1,2,3 delims=:,. " %%A in ('echo %time%') do @(
Set Hour=%%A
Set Min=%%B
Set Sec=%%C
Set Allm=%%A.%%B.%%C
)
@For /F "tokens=3 delims=: " %%A in ('time /t ') do @(
Set AMPM=%%A
)
REM -- Directory details
SET CURR_DATE=%Year%%Month%%Day%%DayW%
SET CURR_TIMESTAMP=%All%_%Allm%%AMPM%
SET CURR_TIMESTAMP1=%Year%%Month%%Day%-%Hour%%Min%%Sec%
SET PROJECT_NAME=TestProcess
echo %CURR_TIMESTAMP%
set "ext=pdf"
for /f "tokens=*" %%a in ('2^>nul dir /b /a-d *.%ext%^|findstr /rv /c:"_[SMTWF][uoehra][neduit]-[01][0-9]-[0-3][0-9]-20[0-9][0-9]_"^|findstr /rvi /c:"_[012][0-9]\.[0-5][0-9]\.[0-5][0-9][AP]M\.%ext%$"') do (
ren """%%a""" """%%~na"""_%PROJECT_NAME%_%CURR_TIMESTAMP%%%~xa
)
Thanks,
San
Re: display time in 12 hr
Posted: 14 Jul 2011 10:41
by aGerman
OK you've got the hour in variable %Hour% from the FOR loop. Now try:
Code: Select all
set /a Hour = Hour %% 12
if %Hour%==0 set "Hour=12"
Regards
aGerman
Re: display time in 12 hr
Posted: 14 Jul 2011 23:36
by kumar_kondapalli
Hi aGerman
I tried using the the code given by you.
Albeit my goal was not achieved, could you please let me know where exactly i need to paste that code.
Mind not am new to bat files dev.
Thanks
San
Re: display time in 12 hr
Posted: 15 Jul 2011 08:45
by kumar_kondapalli
Hey on further investigating i got the date displaying correctly in 12 hr format. But when i include it in my code it says the below error:
Error is as follows:
Fri-07-15-2011_08:11-PM
A duplicate file name exists, or the file
cannot be found.
"Duplicate file name exists"
Can you please help??
@echo off
@For /F "tokens=1,2,3,4 delims=/ " %%A in ('Date /t') do @(
Set DayW=%%A
Set Day=%%B
Set Month=%%C
Set Year=%%D
Set All=%%A-%%B-%%C-%%D
)
for /f "tokens=1 delims=: " %%h in ('time /T') do set hour=%%h
for /f "tokens=2 delims=: " %%m in ('time /T') do set minutes=%%m
for /f "tokens=3 delims=: " %%a in ('time /T') do set ampm=%%a
set NOW=%hour%:%minutes%-%ampm%
@For /F "tokens=3 delims=: " %%A in ('time /t ') do @(
Set AMPM=%%A
)
REM -- Directory details
SET CURR_DATE=%Year%%Month%%Day%%DayW%
SET CURR_TIMESTAMP=%All%_%NOW%
SET CURR_TIMESTAMP1=%Year%%Month%%Day%-%Hour%%Min%%Sec%
SET PROJECT_NAME=TestProcess
echo %CURR_TIMESTAMP%
set "ext=pdf"
for /f "tokens=*" %%a in ('2^>nul dir /b /a-d *.%ext%^|findstr /rv /c:"_[SMTWF][uoehra][neduit]-[01][0-9]-[0-3][0-9]-20[0-
9][0-9]_"^|findstr /rvi /c:"_[012][0-9]\.[0-5][0-9]\.[0-5][0-9][AP]M\.%ext%$"') do (
ren """%%a""" """%%~na"""_%PROJECT_NAME%_%CURR_TIMESTAMP%%%~xa
)
Re: display time in 12 hr
Posted: 15 Jul 2011 09:17
by kumar_kondapalli
i hva fixed this .. thanks a lot for your help guys .. below is the code
@echo off
@For /F "tokens=1,2,3,4 delims=/ " %%A in ('Date /t') do @(
Set DayW=%%A
Set Day=%%B
Set Month=%%C
Set Year=%%D
Set All=%%A-%%B-%%C-%%D
)
for /f "tokens=1 delims=: " %%h in ('time /T') do set hour=%%h
for /f "tokens=2 delims=: " %%m in ('time /T') do set minutes=%%m
for /f "tokens=3 delims=: " %%a in ('time /T') do set ampm=%%a
set NOW=%hour%_%minutes%_%ampm%
REM -- Directory details
SET CURR_DATE=%Year%%Month%%Day%%DayW%
SET CURR_TIMESTAMP=%All%_%NOW%
SET CURR_TIMESTAMP2=%All%
SET CURR_TIMESTAMP1=%Year%%Month%%Day%-%Hour%%Min%%Sec%
SET PROJECT_NAME=TestProcess
echo %CURR_TIMESTAMP%
set "ext=pdf"
for /f "tokens=*" %%a in ('2^>nul dir /b /a-d *.%ext%^|findstr /rv /c:"_[SMTWF][uoehra][neduit]-[01][0-9]-[0-3][0-9]-20[0-
9][0-9]_"^|findstr /rvi /c:"_[012][0-9]\.[0-5][0-9]\.[0-5][0-9][AP]M\.%ext%$"') do (
echo %%a
echo %%~na
echo %%~xa
ren """%%a""" """%%~na"""_%PROJECT_NAME%_%CURR_TIMESTAMP%%%~xa
)
Thanks,
San
Re: display time in 12 hr
Posted: 15 Jul 2011 15:45
by aGerman
But now you lost the seconds
That example shows how my snippet works:
Code: Select all
@echo off &setlocal
for /f "tokens=1-3 delims=:,. " %%A in ("%time%") do (
set "Hour=%%A"
set "Min=%%B"
set "Sec=%%C"
)
set /a Hour = Hour %% 12
if %Hour%==0 set "Hour=12"
set "Allm=%Hour%.%Min%.%Sec%"
echo %Allm%
pause
Regards
aGerman
Re: display time in 12 hr
Posted: 18 Jul 2011 03:27
by kumar_kondapalli
HEY YOU ARE A CHAMP GERMAN THANKS
,
Thanks for your
Thanks,
San
Re: display time in 12 hr
Posted: 03 Aug 2021 03:07
by e2-0-12
aGerman, you forgot the AM or PM. How to do that?
Re: display time in 12 hr
Posted: 03 Aug 2021 11:03
by aGerman
I mean, this topic is 10 years old...
Forget about the %date% and %time% variables if you want to parse datetime values reliably. They depend on local settings.
Use WMIC.
Code: Select all
for /f %%i in ('WMIC OS Get LocalDateTime /value') do for /f %%j in ("%%i") do set "%%j"
echo %LocalDateTime%
set "y=%LocalDateTime:~0,4%"
set "m=%LocalDateTime:~4,2%"
set "d=%LocalDateTime:~6,2%"
set "H=%LocalDateTime:~8,2%"
set "N=%LocalDateTime:~10,2%"
set "S=%LocalDateTime:~12,2%"
echo %y%-%m%-%d%
echo %H%:%N%:%S%
Making a 12-hour time out of it (if you really need it) should be pretty straightforward.
Code: Select all
if "%H%" gtr "11" (set "period=PM") else set "period=AM"
set /a "H=6%H% %% 12"
if %H% equ 0 set "H=12"
if %H% lss 10 set "H=0%H%"
echo %H%:%N%:%S% %period%
Steffen
Re: display time in 12 hr
Posted: 24 Sep 2021 14:19
by atfon
Does anyone know which locale settings determine the format of the time format which robocopy displays without any parameters? On my system, I get 4:16:40 PM, but this is likely not universal. So, with my setup, something like this works:
Code: Select all
for /f "skip=1 tokens=2 delims=," %%e in ('wmic path Win32_LocalTime Get Year 2^>nul /Format:csv') do for /f "tokens=1" %%f in ("%%e") do set "curYr=%%f"
for /f "tokens=5,6,7* delims=: " %%g in ('robocopy ^|findstr "%curYr%"') do (
set "HH=%%h"
set "MS=:%%i:%%j"
)
if %HH% lss 10 set "HH=0%HH%"
set "curTime=%HH%%MS%"
echo %curYr%
Re: display time in 12 hr
Posted: 24 Sep 2021 16:51
by aGerman
atfon wrote: ↑24 Sep 2021 14:19
Does anyone know which locale settings determine the format of the time format which robocopy displays without any parameters?
Likely registry settings
HKEY_CURRENT_USER\Control Panel\International values
sLongDate and
sTimeFormat.
It's not surprising that your code doesn't work for me. German settings use the 24-hour clock.
Steffen
Re: display time in 12 hr
Posted: 25 Sep 2021 08:32
by atfon
aGerman wrote: ↑24 Sep 2021 16:51
It's not surprising that your code doesn't work for me. German settings use the 24-hour clock.
Steffen
Thanks. That's helpful. I'll do some testing.
Re: display time in 12 hr
Posted: 27 Sep 2021 06:07
by atfon
I can now confirm that on my system, these registry keys/control panel settings do indeed control the output of robocopy's date format. Thanks again!