本帖最后由 77七 于 2023-3-19 19:42 编辑
- @echo off
- rem 文件夹不要存在 1.txt 2.txt
- setlocal enabledelayedexpansion
- for /f "tokens=1-4 delims= " %%a in (a.txt) do (
- if defined str (
- >>1.txt echo !str!#%%a#%%b@%%c##%%d
- set "str=%%a#%%b@@%%c##%%d"
- ) else (
- set "str=%%a#%%b@@%%c##%%d"
- )
- )
- set str=
- for /f "delims=" %%a in (b.txt) do (
- if defined str (
- >>2.txt echo !str! %%a
- set str=
- ) else (
- set "str=%%a"
- )
- )
- endlocal
- setlocal enabledelayedexpansion
- for /f "tokens=1-2" %%a in (2.txt) do (
- for /f "tokens=2,6 delims=中#" %%c in ('type "1.txt" ^| findstr "@@%%a" ^| findstr "@%%b"') do (
- set "t1=%%c"
- set "t2=%%d"
- set "t1=!t1:Mar=3!"
- set "t2=!t2:Mar=3!"
- rem 如果需要可以把12个月份的英文都替换为数字
- for /f "tokens=1-4 delims=,. " %%i in ("!t1!") do (
- set "t1date=%%k-%%i-%%j %%l"
- )
- for /f "tokens=1-4 delims=,. " %%i in ("!t2!") do (
- set "t2date=%%k-%%i-%%j %%l"
- )
- if "!t1date!" equ "!t2date!" (
- echo 计算列%%a%%b 时差间隔: OK
- ) else if "!t1date!" gtr "!t2date!" (
- call :t "!t2date!" "!t1date!"
- echo 计算列%%a%%b 时差间隔: !secs!秒
- ) else (
- call :t "!t1date!" "!t2date!"
- echo 计算列%%a%%b 时差间隔: !secs!秒
- )
- )
- )>>c2.txt
- endlocal
- pause
- exit
-
- :t
- ::日期时间差 code by foxjl
- rem set /p date1=输入开始日期(如1984-2-22 5:11:3):
- rem set /p date2=输入结束日期(如2013-9-18 5:6:31):
- set date1=%~1
- set date2=%~2
- for /f "tokens=1,2,3,4,5,6,7 delims=-/:. " %%i in ("%date1%") do ((set Y1=%%i) && (set M1=%%j) && (set D1=%%k) && (set H1=%%l) && (set F1=%%m) && (set S1=%%n) && (set MS1=%%o))
- for /f "tokens=1,2,3,4,5,6,7 delims=-/:. " %%i in ("%date2%") do ((set Y2=%%i) && (set M2=%%j) && (set D2=%%k) && (set H2=%%l) && (set F2=%%m) && (set S2=%%n) && (set MS2=%%o))
- set /a secs=((d2-32075+1461*(y2+4800+(m2-14)/12)/4+367*(m2-2-(m2-14)/12*12)/12-3*((y2+4900+(m2-14)/12)/100)/4)*86400+H2*3600+F2*60+S2)-((d1-32075+1461*(y1+4800+(m1-14)/12)/4+367*(m1-2-(m1-14)/12*12)/12-3*((y1+4900+(m1-14)/12)/100)/4)*86400+H1*3600+F1*60+S1)
- rem set /a D=secs/86400,H=(secs%%86400)/3600,M=(secs%%3600)/60,S=secs%%60
- rem echo.&echo.%date1%与%date2%之间相隔:%D%天%H%时%M%分%S%秒
- rem echo %secs%
- exit /b
复制代码
试试这样行吗?字符串比较不太熟悉,很多地方不知道写的对不对 |