|
voiL
中级用户
  
积分 384
发帖 189
注册 2005-10-19
状态 离线
|
『楼 主』:
(已结)CMD下如何把AM/PM的时间方式更改为24小时制并输入到系
使用 LLM 解释/回答一下
环境:WinXP CMD(CMD Ver 5.1.2600)
我需要向网络内一台叫A1(192.168.0.112)的计算机校对时间,然后把本地时间改成与A1的一样.
然而我用net time之后,输出的文本是如下格式的.
\\192.168.0.112 的当前时间是 2005/11/8 下午 09:47
命令成功完成。
我把09和47滤出来了,也导入到系统了.
但当前时间可是下午的9点多啊,也就是21点多.
请教高手们有没办法把09改成21.
请勿使用第三方工具.
我所写的批处理如下:
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
把运行$Newtime.bat$之后,系统的时间就成了9点多了.
Last edited by voiL on 2005-11-9 at 20:18 ]
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 ]
|
|
2005-11-8 22:13 |
|
|
willsort
元老会员
         Batchinger
积分 4432
发帖 1512
注册 2002-10-18
状态 离线
|
『第 2 楼』:
使用 LLM 解释/回答一下
Re voiL:
试试下面的代码:
@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 ]
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:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得! |
|
2005-11-9 19:06 |
|
|
voiL
中级用户
  
积分 384
发帖 189
注册 2005-10-19
状态 离线
|
『第 3 楼』:
使用 LLM 解释/回答一下
多谢willsort的指点,试用过您给出的代码,确实可行.
只是修改本地时间那里不能实现,必须把"/t"去掉才行.
time /t %_hour%:%3>nul 应当改为 time %_hour%:%3>nul
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
|
|
2005-11-9 20:16 |
|
|
voiL
中级用户
  
积分 384
发帖 189
注册 2005-10-19
状态 离线
|
『第 4 楼』:
使用 LLM 解释/回答一下
测试环境:WinXP CMD(CMD VER 5.1.2600)
经过反复试验,发现willsort兄的代码还有些BUG.
如果192.168.0.112的机子时间是12:00~12:59的时候.
net time \\192.168.0.112得到的结果是:
\\192.168.0.112 的当前时间是 2005/11/10 下午 12:22
命令成功完成。
到这里,willsort兄应该能看出问题了吧??
在经过以下代码运算后,会把时间改成 24:22(一个系统不能接受的时间)
if "%1"=="下午" if "%_hour:~0,1%"=="0" set _hour=%_hour:~-1%
if "%1"=="下午" set /a _hour=%_hour%+12
我把代码改成以下样式之后,就没有出现过同样的问题.
if "%1"=="下午" if "%_hour:~0,1%"=="0" set _hour=%_hour:~-1%
if "%1"=="下午" if "%2"=="12" set /a _hour=%_hour%+12
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
|
|
2005-11-10 12:45 |
|
|
willsort
元老会员
         Batchinger
积分 4432
发帖 1512
注册 2002-10-18
状态 离线
|
『第 5 楼』:
使用 LLM 解释/回答一下
Re voiL:
甚为欣赏兄实际测试的行动和独立思索的意识,希望能与兄多多交流。
time /t 的问题确实是我的疏忽,因为我在 xp_cmd 下使用 time 时习惯上使用 time /t 的格式;12点的问题我也曾有考虑,但是因为在我的印象中系统不会给出下午12点的时间,而是会给出上午0点的时间,因此最终对此未作处理。
说到底都是测试的问题,因为我不想再测试中更改我的系统时钟,所以 time /t 一句是写作 echo time /t %_hour%:%3 的,测得显示结果没有问题后,就将代码改成上述模样直接贴出了;而12点问题,因为自己的系统不是12时制的,所以也就偷懒未作测试了。许许多多的事实一再证实,不经测试或测试不周的代码是绝对靠不住的!可惜很多得到论坛所贴代码的朋友不会理睬这些,所以这才显示出兄进行实际的和周到的测试的可贵。
另外,你帖子中最后代码的第二句是否应改为:
if "%1"=="下午" if not "%2"=="12" set /a _hour=%_hour%+12
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:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得! |
|
2005-11-10 19:45 |
|
|
voiL
中级用户
  
积分 384
发帖 189
注册 2005-10-19
状态 离线
|
『第 6 楼』:
使用 LLM 解释/回答一下
多谢willsort兄指点.
我有个请求,希望大哥能给予帮助:
能否把以下代码中红色部分说明一下,如果可以的话,能否把if中与之相关的资料给我一份?因为我在CMD下查不到这些资料.在google上翻了十几页也找不到.
if "%1"=="下午" if "%_hour:~0,1%"=="0" set _hour=%_hour:~-1%
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%
|
|
2005-11-11 19:13 |
|
|
willsort
元老会员
         Batchinger
积分 4432
发帖 1512
注册 2002-10-18
状态 离线
|
『第 7 楼』:
使用 LLM 解释/回答一下
Re voiL:
红色字体所示的代码是环境变量的引用特性,与 if 无关。请查阅命令环境变量设置命令 set 的帮助文档,简单的用法是在CMD下使用 set /?。
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:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得! |
|
2005-11-12 18:44 |
|
|
voiL
中级用户
  
积分 384
发帖 189
注册 2005-10-19
状态 离线
|
|
2005-11-12 22:31 |
|