China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-08-01 13:44
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » (Closed) How to change the AM/PM time format to 24-hour format in CMD and input to the system View 2,436 Replies 7
Original Poster Posted 2005-11-08 22:13 ·  中国 广东 肇庆 四会市 电信
中级用户
★★
Credits 384
Posts 189
Joined 2005-10-19 13:12
20-year member
UID 43709
Gender Male
Status Offline
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 ]
Floor 2 Posted 2005-11-09 19:06 ·  中国 山西 大同 中移铁通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
Re voiL:

Try the following code:


@echo off
for /f "tokens=4,5,6 delims=: " %%a in ('net time \\192.168.0.112') do call:settime %%a %%b %%c
goto:eof

:settime
set _hour=%2
if "%1"=="下午" if "%_hour:~0,1%"=="0" set _hour=%_hour:~-1%
if "%1"=="下午" set /a _hour=%_hour%+12
set voltime=%_hour%%3>nul
time /t %_hour%:%3>nul
set _hour=


[ Last edited by willsort on 2005-11-9 at 19:07 ]
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 3 Posted 2005-11-09 20:16 ·  中国 广东 肇庆 四会市 电信
中级用户
★★
Credits 384
Posts 189
Joined 2005-10-19 13:12
20-year member
UID 43709
Gender Male
Status Offline
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
Floor 4 Posted 2005-11-10 12:45 ·  中国 广东 肇庆 四会市 电信
中级用户
★★
Credits 384
Posts 189
Joined 2005-10-19 13:12
20-year member
UID 43709
Gender Male
Status Offline
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
Floor 5 Posted 2005-11-10 19:45 ·  中国 山西 临汾 中移铁通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
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
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 6 Posted 2005-11-11 19:13 ·  中国 广东 肇庆 四会市 电信
中级用户
★★
Credits 384
Posts 189
Joined 2005-10-19 13:12
20-year member
UID 43709
Gender Male
Status Offline
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%
Floor 7 Posted 2005-11-12 18:44 ·  中国 山西 运城 中移铁通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
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.
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 8 Posted 2005-11-12 22:31 ·  中国 广东 肇庆 四会市 电信
中级用户
★★
Credits 384
Posts 189
Joined 2005-10-19 13:12
20-year member
UID 43709
Gender Male
Status Offline
Forum Jump: