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 08:35
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Batch script to convert seconds into days, hours, minutes and seconds View 5,042 Replies 24
Floor 16 Posted 2007-01-17 09:52 ·  中国 广东 深圳 福田区 电信
中级用户
★★
蝴蝶之吻
Credits 430
Posts 177
Joined 2006-09-20 12:00
19-year member
UID 63170
From 广东深圳
Status Offline
If I enter a letter, it still appears:


Please enter the number of seconds: dddd

0 days 0 hours 0 minutes 0 seconds

Press any key to continue...


Am I here to cause trouble,,, hehehe... ;)

[ Last edited by zouzhxi on 2007-1-17 at 09:54 AM ]
Floor 17 Posted 2007-01-17 11:02 ·  美国 弗吉尼亚州 华盛顿县 Microsoft
高级用户
★★
Credits 783
Posts 268
Joined 2006-12-26 17:18
19-year member
UID 74627
Gender Male
Status Offline
Understand the above code as (not milliseconds):

set /a D=%t%/86400
set /a H=%t%%%86400/3600
set /a M=%t%%%86400%%3600/60
set /a S=%t%%%86400%%3600%%60
ECHO %D% days %H% hours %M% minutes %S% seconds

The perception is: The value is t divided by 86400 seconds (equal to 1 day), the quotient is the number of days, the remainder is divided by 3600 seconds (equal to 1 hour), the quotient is the number of hours, and the remainder is cycled again, and so on, which can be extended to weeks, months, and years. But due to the 32-bit precision limit, it will not achieve the maximum expected effect
Floor 18 Posted 2007-01-18 01:55
中级用户
★★
DOS之日
Credits 337
Posts 161
Joined 2006-11-04 05:27
19-year member
UID 69523
Gender Male
Status Offline
Originally posted by namejm at 2007-1-16 16:15:
  The code on floor 12 has the following defects:

  1. Using a replace 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 ...


Thanks for pointing it out, hehe. It was written carelessly. It has been corrected.
for /f %%h in (`echo hxuan`) do for /f %%x in (`echo hxuan`) do if %%h==%%x nul
Floor 19 Posted 2007-01-18 07:15 ·  中国 广东 广州 天河区 电信
金牌会员
★★★★
一叶枝头,万树皆春
Credits 2,564
Posts 1,127
Joined 2006-12-25 22:57
19-year member
UID 74552
Gender Male
Status Offline
Originally posted by PPdos at 2007-1-16 08:39 PM:
You guys are amazing. You even got the milliseconds accurate. It seems it can be modified into a Spring Festival countdown^^



@ECHO OFF&MODE CON COLS=40 LINES=3&TITLE=New Year Countdown
SETLOCAL ENABLEDELAYEDEXPANSION
SET /A YEAR=%DATE:~0,4%,MONTH=%DATE:~5,2%,DAY=%DATE:~8,2%
CALL :ALG MONTH %MONTH%&&CALL :ALG DAY %DAY%
IF NOT "%1" == "" SET NB=%1&&GOTO :TOPY
SET /A NX=%RANDOM%%%12+1,NZ=%RANDOM%%%28+1
SET NYY=%NX%.%NZ%
:TOPX
CLS&ECHO.&SET /P NB=%NYY%过年?
IF "%NB%" == "" SET NB=%NYY%&&GOTO TOPZ
IF /I "%NB:~0,1%" == "n" SET NYY=Then when is it&&GOTO TOPX
IF /I "%NB:~0,1%" == "y" SET NB=%NYY%&&GOTO TOPY
IF NOT "%NB%" == "" SET NYY=%NB%&&GOTO TOPX
:TOPY
ECHO %NB%|FINDSTR /R "^**">nul||GOTO TOPX
FOR /F "tokens=1,2 delims=.:;-/" %%i IN ("%NB%") DO CALL :ALG BM %%i&CALL :ALG BD %%j
IF NOT %BM% GTR 0 (GOTO TOP) ELSE IF %BM% GTR 12 GOTO TOPX
IF NOT %BD% GTR 0 (GOTO TOP) ELSE IF %BD% GTR 31 GOTO TOPX
FOR /L %%i IN (1,1,12) DO (
if %%i geq 8 (set d1=30&&set d2=31) else set d1=31&&set d2=30
set /a st=%%i%%2
if !st! equ 0 (set M%%i=!d2!) else set M%%i=!d1!)
CALL :LM %YEAR%
IF %BM% GTR %MONTH% SET GO=GTR
IF %BM% EQU %MONTH% SET GO=EQU
IF %BM% LSS %MONTH% SET /A NEWYEAR=%YEAR%+1&&SET GO=LSS
GOTO %GO%

:GTR
FOR /L %%i IN (%MONTH%,1,%BM%) DO SET /A DALL=!DALL!+M%%i
SET /A DALL=%DALL%+!M%MONTH%!-%DAY%+%BD%-M%MONTH%-M%BM%
GOTO TIME

:EQU
IF %BD% GTR %DAY% SET /A DALL=%BD%-%DAY%&&GOTO TIME
IF %BD% EQU %DAY% SET DALL=0&&GOTO TIME
IF %BD% LSS %DAY% SET /A NERA=%DAY%-%BD%&&GOTO LSS

:LSS
FOR /L %%i IN (%MONTH%,1,12) DO SET /A OALL+=M%%i
SET /A OALL=%OALL%+!M%MONTH%!-%DAY%-M%MONTH%
CALL :LM %NEWYEAR%
FOR /L %%i IN (1,1,%BM%) DO SET /A NALL+=M%%i
SET /A NALL+=%BD%-M%BM%
SET /A DALL=%OALL%+%NALL%

:TIME
FOR /F "tokens=1-4 delims=:." %%a IN ("%TIME%") DO (
CALL :ALG TT %%b&&CALL :ALG TS %%c&&CALL :ALG TM %%d
SET /A TH=24-%%a-1,TT=60-!TT!-1,TS=60-!TS!-1,TM=1000-!TM!*10
CLS&ECHO/&ECHO There are %DALL% days!TH! hours!TT! minutes!TS! seconds!TM! milliseconds until New Year)
IF "%DAY%" == "%DATE:~8,2%" (GOTO TIME) ELSE GOTO END

:END
START /I "" %0 "NB"
GOTO :EOF

:LM
SET /A LM=(%1-2000)%%4
IF NOT %LM% EQU 0 (SET M2=28) ELSE SET M2=29
GOTO :EOF

:ALG
SET CHKALG=%2
IF "%CHKALG:~1,1%" == "" SET %1=%CHKALG%&GOTO :EOF
SET /A %1=%CHKALG:~0,1%*10+%CHKALG:~-1%
GOTO :EOF


I just wrote this hehe :D
Floor 20 Posted 2007-10-05 10:21 ·  中国 上海 长宁区 电信
中级用户
★★
Credits 400
Posts 211
Joined 2007-09-30 18:02
18-year member
UID 98650
Gender Male
Status Offline
Is there a way to break through the 11-day limit? If the number is too large, it will overflow. Thanks!

Please enter the number of seconds (press Enter directly to exit): 4321432531252
Invalid number. Numbers are limited to 32-bits of precision.
Invalid number. Numbers are limited to 32-bits of precision.
Invalid number. Numbers are limited to 32-bits of precision.
Invalid number. Numbers are limited to 32-bits of precision.

11 days 32 hours 19 minutes 47 seconds
Floor 21 Posted 2007-10-05 10:27 ·  中国 上海 长宁区 电信
中级用户
★★
Credits 400
Posts 211
Joined 2007-09-30 18:02
18-year member
UID 98650
Gender Male
Status Offline
This forum is really good!!! Strongly support!

@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
if %DD% GEQ 7 set /a DD=DD%%7
set /a WW=%input%/604800%%60
echo.
if %DD% GEQ 1 (echo %WW% weeks %DD% days %HH% hours %MM% minutes %SS% seconds
) else (
echo %DD% days %HH% hours %MM% minutes %SS% seconds
)
echo.
pause
goto begin

[ Last edited by uiopuiop on 2007-10-5 at 11:28 AM ]
Floor 22 Posted 2007-10-05 10:33 ·  中国 上海 长宁区 电信
中级用户
★★
Credits 400
Posts 211
Joined 2007-09-30 18:02
18-year member
UID 98650
Gender Male
Status Offline
However...

Please enter the number of seconds (press Enter directly to exit): 65356534654
Invalid number. Numbers are limited to 32-bits of precision.
Invalid number. Numbers are limited to 32-bits of precision.
Invalid number. Numbers are limited to 32-bits of precision.
Invalid number. Numbers are limited to 32-bits of precision.
Invalid number. Numbers are limited to 32-bits of precision.

29 weeks 25 days 10 hours 33 minutes 20 seconds
Floor 23 Posted 2007-10-05 11:53 ·  中国 上海 长宁区 电信
中级用户
★★
Credits 400
Posts 211
Joined 2007-09-30 18:02
18-year member
UID 98650
Gender Male
Status Offline
Please enter the number of seconds (press Enter directly to exit): 3431253

5 weeks 4 days 5 hours 7 minutes 33 seconds

It seems it should be 17 hours, not 5 hours...

[ Last edited by uiopuiop on 2007-10-5 at 12:04 PM ]
Floor 24 Posted 2007-10-05 12:25 ·  中国 湖北 武汉 中国科学院武汉分院
银牌会员
★★★
Credits 1,187
Posts 555
Joined 2006-12-21 07:35
19-year member
UID 74129
Gender Male
Status Offline
Floor 25 Posted 2007-10-05 12:29 ·  中国 上海 长宁区 电信
中级用户
★★
Credits 400
Posts 211
Joined 2007-09-30 18:02
18-year member
UID 98650
Gender Male
Status Offline
Your algorithm is very good, I am stupid. How to input a number in seconds and output it with precision to seconds only. And the code is in a batch file.

Can you write it out, thank you very much!

[ Last edited by uiopuiop on 2007-10-5 at 12:54 PM ]
Forum Jump: