中国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-08-10 15:59
47,811 topics / 349,897 posts / today 0 new / 48,255 members
DOS批处理 & 脚本技术(批处理室) » Batch script to convert seconds into days, hours, minutes and seconds
Printable Version  5,268 / 24
Floor1 PPdos Posted 2007-01-16 22:15
高级用户 Posts 268 Credits 783
A C programming problem. Please do it with batch processing. For example:

Input (seconds): 3662

Display: 0 days 1 hour 1 minute 2 seconds
Floor2 namejm Posted 2007-01-16 23:28
荣誉版主 Posts 1,737 Credits 5,226 From 成都
The following is the simplified code:

```batch
@echo off
:begin
cls
set /a D=0,H=0,M=0,num=
set /p num= Please enter the number of seconds (press Enter directly to exit):
if not defined num exit
call :transform D %num% 86400
call :transform H %num% 3600
call :transform M %num% 60
echo %D% days %H% hours %M% minutes %num% seconds
pause
goto begin

:transform
set /a %1=%2/%3
if %1 gtr 0 set /a num=%2-%3*%1
goto :eof
```
Floor3 zh159 Posted 2007-01-16 23:40
金牌会员 Posts 1,467 Credits 3,687
```batch脚本代码,功能是获取用户输入的秒数,然后将其转换为天、小时、分钟和秒并输出。具体来说:
首先`@echo off`关闭命令回显,`:begin`是标签,`cls`清屏,然后获取用户输入的秒数,根据输入进行计算,分别得出天、小时、分钟、秒,最后输出并暂停后回到`:begin`标签继续循环。
代码内容如下:
```
@echo off
:begin
cls
echo.
set input=
set /p input= 请输入秒数(退出请直接按回车):
if not defined input exit
set /a SS=%input%%%60
set /a MM=%input%/60%%60
set /a HH=%input%/3600%%60
set /a DD=%input%/86400%%60
echo.
echo %DD% 天 %HH% 小时 %MM% 分 %SS% 秒
echo.
pause
goto begin
```
Floor4 namejm Posted 2007-01-16 23:48
荣誉版主 Posts 1,737 Credits 5,226 From 成都
Hehe, Brother zh159's thinking is more straightforward! I overcomplicated it.
Floor5 redtek Posted 2007-01-16 23:50
金牌会员 Posts 1,147 Credits 2,902
Wonderful!!! Top~
Floor6 scriptor Posted 2007-01-17 00:02
银牌会员 Posts 555 Credits 1,187
A simple math problem ah
Hehe

The program should also be simple
Floor7 scriptor Posted 2007-01-17 00:08
银牌会员 Posts 555 Credits 1,187
If the input is xxxxx.34 and milliseconds need to be considered, then the code of the 2nd and 3rd floors cannot be executed

[ Last edited by scriptor on 2007-1-16 at 11:09 AM ]
Floor8 namejm Posted 2007-01-17 00:56
荣誉版主 Posts 1,737 Credits 5,226 From 成都
When calculating the number of days on the 3rd floor, dividing by 60 to get the remainder will cause problems. The %%60 should be removed.
Floor9 zh159 Posted 2007-01-17 02:28
金牌会员 Posts 1,467 Credits 3,687
At that time, I wrote it casually , correction:


With milliseconds:
PS: Milliseconds are 1000 to 1 second


[ Last edited by zh159 on 2007-1-16 at 09:42 PM ]
Floor10 hxuan999 Posted 2007-01-17 02:37
中级用户 Posts 161 Credits 337
There is a problem when dealing with milliseconds. For example, if you enter 5485.3, then.3 should be 300 milliseconds, not 3 milliseconds.
Floor11 zh159 Posted 2007-01-17 02:46
金牌会员 Posts 1,467 Credits 3,687
Originally posted by hxuan999 at 2007-1-16 13:37:
There is a problem when dealing with milliseconds. For example, entering 5485.3, then.3 should be 300 milliseconds, not 3 milliseconds.

Forgot -_-|||
Thanks for the reminder, it has been corrected, but it still cannot handle more than 4 decimal places

[ Last edited by zh159 on 2007-1-16 at 01:48 PM ]
Floor12 hxuan999 Posted 2007-01-17 02:54
中级用户 Posts 161 Credits 337
I'll also do one with milliseconds (milliseconds with more than four decimal places are truncated to 000.00 milliseconds):


  1. @echo off&setlocal enabledelayedexpansion
  2. :begin
  3. cls
  4. echo;
  5. set input=
  6. set /p input= Please enter the number of seconds (press Enter directly to exit):
  7. if not defined input exit
  8. for /f "tokens=1* delims=." %%a in ("%input%") do set input=%%a & set D5=%%b00000
  9. for /l %%i in (4,-1,2) do set /a "D%%i=!input!%%60" && set /a "input=!input!/60"
  10. set "D5=!D5:~0,3!.!D5:~3,2!"
  11. set /a "D1=!input!%%24"
  12. echo;&echo; %D1% days %D2% hours %D3% minutes %D4% seconds %D5% milliseconds&echo;
  13. pause
  14. goto begin
hxuan: 2007-01-17 12:52


This program has been corrected.

[ Last edited by hxuan999 on 2007-1-17 at 01:59 PM ]
Floor13 namejm Posted 2007-01-17 04:15
荣誉版主 Posts 1,737 Credits 5,226 From 成都
The code on floor 12 has the following defects:

1. Using the replacement statement to intercept the decimal part is error-prone. For example, when the integer part is a certain string of the decimal part, such as 23.1234. It is recommended not to use such an unconventional statement, and still use delims=. in the for statement to extract it;
2. When calculating the number of days, it is still divided by 60, which is incorrect.
Floor14 tao0610 Posted 2007-01-17 07:22
高级用户 Posts 218 Credits 579
```
@echo off
:start
setlocal
cls&echo.
set/p input=Please enter the number of seconds:
for /f "tokens=1,2 delims=." %%a in ("%input%") do set input=%%a&set input1=%%b
for /l %%a in (1,1,2) do set/a T%%a=input%%60,input/=60
set/a T3=input%%24,T4=input/24
set input1=%input1%0000
for /f "tokens=* delims=0" %%a in ("%input1:~0,4%") do set T0=%%a
echo/
if defined T0 (echo %T4% days %T3% hours %T2% minutes %T1% seconds %T0% milliseconds
) else (echo %T4% days %T3% hours %T2% minutes %T1% seconds)
echo\
pause
endlocal
goto start

```
Floor15 PPdos Posted 2007-01-17 09:39
高级用户 Posts 268 Credits 783
You guys are amazing. You even got the milliseconds accurate. It seems it can be modified into a New Year's countdown to the Spring Festival^^
1 2  Next
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023