display time in 12 hr

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
kumar_kondapalli
Posts: 32
Joined: 08 Jul 2011 03:38

display time in 12 hr

#1 Post by kumar_kondapalli » 14 Jul 2011 01:40

HI ,

Can we display time in 12 hr format using batch file

thanks,
san

nitt
Posts: 218
Joined: 22 Apr 2011 02:43

Re: display time in 12 hr

#2 Post by nitt » 14 Jul 2011 09:33

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

kumar_kondapalli
Posts: 32
Joined: 08 Jul 2011 03:38

Re: display time in 12 hr

#3 Post by kumar_kondapalli » 14 Jul 2011 10:03

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

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: display time in 12 hr

#4 Post by aGerman » 14 Jul 2011 10:41

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

kumar_kondapalli
Posts: 32
Joined: 08 Jul 2011 03:38

Re: display time in 12 hr

#5 Post by kumar_kondapalli » 14 Jul 2011 23:36

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

kumar_kondapalli
Posts: 32
Joined: 08 Jul 2011 03:38

Re: display time in 12 hr

#6 Post by kumar_kondapalli » 15 Jul 2011 08:45

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
)

kumar_kondapalli
Posts: 32
Joined: 08 Jul 2011 03:38

Re: display time in 12 hr

#7 Post by kumar_kondapalli » 15 Jul 2011 09:17

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

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: display time in 12 hr

#8 Post by aGerman » 15 Jul 2011 15:45

But now you lost the seconds :wink:
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

kumar_kondapalli
Posts: 32
Joined: 08 Jul 2011 03:38

Re: display time in 12 hr

#9 Post by kumar_kondapalli » 18 Jul 2011 03:27

HEY YOU ARE A CHAMP GERMAN THANKS :) :D ,

Thanks for your :idea:

Thanks,
San

e2-0-12
Posts: 1
Joined: 03 Aug 2021 03:05

Re: display time in 12 hr

#10 Post by e2-0-12 » 03 Aug 2021 03:07

aGerman, you forgot the AM or PM. How to do that?

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: display time in 12 hr

#11 Post by aGerman » 03 Aug 2021 11:03

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

atfon
Posts: 178
Joined: 06 Oct 2017 07:33

Re: display time in 12 hr

#12 Post by atfon » 24 Sep 2021 14:19

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%

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: display time in 12 hr

#13 Post by aGerman » 24 Sep 2021 16:51

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

atfon
Posts: 178
Joined: 06 Oct 2017 07:33

Re: display time in 12 hr

#14 Post by atfon » 25 Sep 2021 08:32

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.

atfon
Posts: 178
Joined: 06 Oct 2017 07:33

Re: display time in 12 hr

#15 Post by atfon » 27 Sep 2021 06:07

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!

Post Reply