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-06-25 06:50
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Batch script to convert seconds into days, hours, minutes and seconds View 5,040 Replies 24
Original Poster Posted 2007-01-16 22:15 ·  美国 弗吉尼亚州 华盛顿县 Microsoft
高级用户
★★
Credits 783
Posts 268
Joined 2006-12-26 17:18
19-year member
UID 74627
Gender Male
Status Offline
A C programming problem. Please do it with batch processing. For example:

Input (seconds): 3662

Display: 0 days 1 hour 1 minute 2 seconds
Floor 2 Posted 2007-01-16 23:28 ·  中国 广东 电信
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
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
```
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
PPdos +2 2007-01-17 10:27
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 3 Posted 2007-01-16 23:40 ·  中国 广西 玉林 博白县 电信
金牌会员
★★★★
Credits 3,687
Posts 1,467
Joined 2005-08-08 12:00
20-year member
UID 44210
Status Offline
```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
```
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
PPdos +2 2007-01-17 10:22
Floor 4 Posted 2007-01-16 23:48 ·  中国 广东 电信
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
Hehe, Brother zh159's thinking is more straightforward! I overcomplicated it.
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 5 Posted 2007-01-16 23:50 ·  中国 北京 东城区 联通
金牌会员
★★★★
Credits 2,902
Posts 1,147
Joined 2006-09-21 12:00
19-year member
UID 63324
Gender Male
Status Offline
Wonderful!!! Top~
    Redtek,一个永远在网上流浪的人……

_.,-*~'`^`'~*-,.__.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._
Floor 6 Posted 2007-01-17 00:02 ·  中国 北京 中国科学院研究生院
银牌会员
★★★
Credits 1,187
Posts 555
Joined 2006-12-21 07:35
19-year member
UID 74129
Gender Male
Status Offline
A simple math problem ah
Hehe

The program should also be simple
Floor 7 Posted 2007-01-17 00:08 ·  中国 北京 中国科学院研究生院
银牌会员
★★★
Credits 1,187
Posts 555
Joined 2006-12-21 07:35
19-year member
UID 74129
Gender Male
Status Offline
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 ]
Floor 8 Posted 2007-01-17 00:56 ·  中国 广东 电信
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
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.
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 9 Posted 2007-01-17 02:28 ·  中国 广西 玉林 博白县 电信
金牌会员
★★★★
Credits 3,687
Posts 1,467
Joined 2005-08-08 12:00
20-year member
UID 44210
Status Offline
At that time, I wrote it casually , correction:
@echo off
:begin
cls
echo.
set input=
set /p input= Please enter the number of seconds (press Enter directly to exit):
if not defined input exit
set /a SS=%input%%%60
set /a MM=%input%/60%%60
set /a HH=%input%/3600%%60
if %HH% GEQ 24 set /a HH=HH%%24
set /a DD=%input%/86400
echo.
echo %DD% days %HH% hours %MM% minutes %SS% seconds
echo.
pause
goto begin


With milliseconds:
PS: Milliseconds are 1000 to 1 second
@echo off
:begin
cls
echo.
set input=
set /p input= Please enter the number of seconds (press Enter directly to exit):
if not defined input exit
for /f "tokens=1* delims=." %%i in ("%input%") do set input=%%i&&set .n=%%j
if "%.n%" == "" set .n=0
if "%.n:~1%" == "" set .n=%.n%0
if "%.n:~2%" == "" set .n=%.n%0
if "%.n:~0,1%" == "0" set .n=%.n:~1%
if "%.n:~0,1%" == "0" set .n=%.n:~1%


set /a SS=%input%%%60
set /a MM=%input%/60%%60
set /a HH=%input%/3600%%60
if %HH% GEQ 24 set /a HH=HH%%24
set /a DD=%input%/86400
echo.
echo %DD% days %HH% hours %MM% minutes %SS% seconds %.n% milliseconds
echo.
pause
goto begin


[ Last edited by zh159 on 2007-1-16 at 09:42 PM ]
Floor 10 Posted 2007-01-17 02:37
中级用户
★★
DOS之日
Credits 337
Posts 161
Joined 2006-11-04 05:27
19-year member
UID 69523
Gender Male
Status Offline
There is a problem when dealing with milliseconds. For example, if you enter 5485.3, then.3 should be 300 milliseconds, not 3 milliseconds.
for /f %%h in (`echo hxuan`) do for /f %%x in (`echo hxuan`) do if %%h==%%x nul
Floor 11 Posted 2007-01-17 02:46 ·  中国 广西 玉林 博白县 电信
金牌会员
★★★★
Credits 3,687
Posts 1,467
Joined 2005-08-08 12:00
20-year member
UID 44210
Status Offline
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 ]
Floor 12 Posted 2007-01-17 02:54
中级用户
★★
DOS之日
Credits 337
Posts 161
Joined 2006-11-04 05:27
19-year member
UID 69523
Gender Male
Status Offline
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 ]
for /f %%h in (`echo hxuan`) do for /f %%x in (`echo hxuan`) do if %%h==%%x nul
Floor 13 Posted 2007-01-17 04:15 ·  中国 广东 电信
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
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.
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 14 Posted 2007-01-17 07:22 ·  中国 北京 朝阳区 联通
高级用户
★★
朦胧的世界
Credits 579
Posts 218
Joined 2006-10-24 04:29
19-year member
UID 67972
Status Offline
```
@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

```

认识自己,降伏自己,改变自己
,才能改变别人!
Floor 15 Posted 2007-01-17 09:39 ·  美国 弗吉尼亚州 华盛顿县 Microsoft
高级用户
★★
Credits 783
Posts 268
Joined 2006-12-26 17:18
19-year member
UID 74627
Gender Male
Status Offline
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^^
Forum Jump: