中国DOS联盟论坛

China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --
Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
Guest | Log in | Register | Members | Search | China DOS Union
中国DOS联盟论坛
The time now is 2026-09-23 16:04
47,812 topics / 349,917 posts / today 0 new / 48,273 members
DOS批处理 & 脚本技术(批处理室) » (Closed) How to change the AM/PM time format to 24-hour format in CMD and input to the system
Printable Version  2,609 / 7
Floor1 voiL Posted 2005-11-08 22:13
中级用户 Posts 189 Credits 384
Environment: WinXP CMD (CMD Ver 5.1.2600)

I need to synchronize the time with a computer named A1 (192.168.0.112) in the network, and then change the local time to be the same as A1's.

However, after I used net time, the output text is in the following format.

The current time of \\192.168.0.112 is 2005/11/8 9:47 PM

The command completed successfully.


I filtered out 09 and 47, and also imported them into the system.

But the current time is more than 9 PM, that is, more than 21:00.

Ask experts if there is a way to change 09 to 21.

Do not use third-party tools.

The batch script I wrote is as follows:

net time \\192.168.0.112>%temp%\$112$.tmp
FOR /F "tokens=5,6* delims=: " %%a in (%temp%\$112$.tmp) do (Set voltime=%%a%%b) & echo time %%a:%%b>%temp%\$Newime$.bat
Start %temp%\$Newtime$.bat
Del /Q %temp%\$Newtime$.bat


After running $Newtime.bat$, the system time becomes 9:00 something.

[ Last edited by voiL on 2005-11-9 at 20:18 ]
Floor2 willsort Posted 2005-11-09 19:06
元老会员 Posts 1,512 Credits 4,432
Re voiL:

Try the following code:



[ Last edited by willsort on 2005-11-9 at 19:07 ]
Floor3 voiL Posted 2005-11-09 20:16
中级用户 Posts 189 Credits 384
Thanks to willsort's guidance, I have tried the code you provided, and it does work.

Only the part of modifying the local time cannot be implemented, and the "/t" must be removed.

time /t %_hour%:%3>nul should be changed to time %_hour%:%3>nul
Floor4 voiL Posted 2005-11-10 12:45
中级用户 Posts 189 Credits 384
Test environment: WinXP CMD (CMD VER 5.1.2600)

After repeated trials, it was found that there are still some BUGs in Brother willsort's code.

When the time of the machine at 192.168.0.112 is from 12:00 to 12:59.

The result obtained by net time \\192.168.0.112 is:

The current time of \\192.168.0.112 is 2005/11/10 下午 12:22

The command completed successfully.


Up to here, Brother willsort should be able to see the problem.

After the following code operation, the time will be changed to 24:22 (a time that the system cannot accept)

if "%1"=="下午" if "%_hour:~0,1%"=="0" set _hour=%_hour:~-1%
if "%1"=="下午" set /a _hour=%_hour%+12


After I changed the code to the following style, the same problem did not occur again.

if "%1"=="下午" if "%_hour:~0,1%"=="0" set _hour=%_hour:~-1%
if "%1"=="下午" if "%2"=="12" set /a _hour=%_hour%+12
Floor5 willsort Posted 2005-11-10 19:45
元老会员 Posts 1,512 Credits 4,432
Re voiL:

I greatly appreciate your practical testing actions and independent thinking awareness. I hope to communicate with you more.

The issue with time /t was indeed my oversight because when I use time in xp_cmd, I am accustomed to using the format time /t; I also considered the 12 o'clock issue, but because in my impression the system doesn't give the time for 12 p.m., but instead gives the time for 0 a.m., so I finally didn't handle this.

In the final analysis, it's all a matter of testing. Because I didn't want to change my system clock during testing, so the line time /t was written as echo time /t %_hour%:%3. After the display result was measured to be correct, I directly posted the code in the above form; and for the 12 o'clock issue, because my system is not in 12-hour format, I was also lazy and didn't test it. Countless facts repeatedly confirm that code that is not tested or tested inadequately is absolutely unreliable! Unfortunately, many friends who get the code posted on the forum won't pay attention to these, so this is where your可贵 (valuable) actual and thorough testing shows.

In addition, should the second sentence of the code at the end of your post be changed to:

if "%1"=="下午" if not "%2"=="12" set /a _hour=%_hour%+12
Floor6 voiL Posted 2005-11-11 19:13
中级用户 Posts 189 Credits 384
Thanks to brother willsort for the guidance.

I have a request and hope the elder brother can give assistance:

Can you explain the part in red in the following code? If possible, can you give me a copy of the relevant materials in the if? Because I can't find these materials in CMD. I turned over more than a dozen pages on google and couldn't find them.

if "%1"=="下午" if "%_hour:~0,1%"=="0" set _hour=%_hour:~-1%
Floor7 willsort Posted 2005-11-12 18:44
元老会员 Posts 1,512 Credits 4,432
Re voiL:

The code shown in red is the reference feature of environment variables, which has nothing to do with if. Please refer to the help document of the command environment variable setting command set. The simple usage is to use set /? under CMD.
Floor8 voiL Posted 2005-11-12 22:31
中级用户 Posts 189 Credits 384
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023