display time in 12 hr
Moderator: DosItHelp
-
- Posts: 32
- Joined: 08 Jul 2011 03:38
display time in 12 hr
HI ,
Can we display time in 12 hr format using batch file
thanks,
san
Can we display time in 12 hr format using batch file
thanks,
san
Re: display time in 12 hr
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
-
- Posts: 32
- Joined: 08 Jul 2011 03:38
Re: display time in 12 hr
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
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
OK you've got the hour in variable %Hour% from the FOR loop. Now try:
Regards
aGerman
Code: Select all
set /a Hour = Hour %% 12
if %Hour%==0 set "Hour=12"
Regards
aGerman
-
- Posts: 32
- Joined: 08 Jul 2011 03:38
Re: display time in 12 hr
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
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
-
- Posts: 32
- Joined: 08 Jul 2011 03:38
Re: display time in 12 hr
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
)
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
)
-
- Posts: 32
- Joined: 08 Jul 2011 03:38
Re: display time in 12 hr
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
@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
But now you lost the seconds
That example shows how my snippet works:
Regards
aGerman
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
-
- Posts: 32
- Joined: 08 Jul 2011 03:38
Re: display time in 12 hr
HEY YOU ARE A CHAMP GERMAN THANKS ,
Thanks for your
Thanks,
San
Thanks for your
Thanks,
San
Re: display time in 12 hr
aGerman, you forgot the AM or PM. How to do that?
Re: display time in 12 hr
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.
Making a 12-hour time out of it (if you really need it) should be pretty straightforward.
Steffen
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%
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%
Re: display time in 12 hr
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
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
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!