Page 1 of 1
Date/Time Comparison
Posted: 25 Dec 2021 22:43
by AR Coding
Hi,
i have looked all over this forum but i cant seem to find what im looking for.
I have a date in the following format:
i want to know if it is possible to do a date/time comparison and check if another date/time in the same format is older/newer than the specified one
Can someone please help me?
Thank you
Re: Date/Time Comparison
Posted: 26 Dec 2021 15:36
by aGerman
Time stamps using format yyyyMMddHHmmSS are suitable for those comparisons (provided the 24 hours clock is used).
Code: Select all
@echo off &setlocal EnableDelayedExpansion
set "dt1=Fri, 27 Aug 2021 12:37:30 GMT"
set "dt2=Fri, 27 Aug 2021 12:37:31 GMT"
for %%i in ("Jan=01" "Feb=02" "Mar=03" "Apr=04" "May=05" "Jun=06" "Jul=07" "Aug=08" "Sep=09" "Oct=10" "Nov=11" "Dec=12") do set %%i
for /f "tokens=2-7 delims=: " %%i in ("%dt1%") do set "timestamp1=%%k!%%j!%%i%%l%%m%%n"
for /f "tokens=2-7 delims=: " %%i in ("%dt2%") do set "timestamp2=%%k!%%j!%%i%%l%%m%%n"
echo %timestamp1%
echo %timestamp2%
pause
The older time stamp is the lesser.
Steffen
Re: Date/Time Comparison
Posted: 26 Dec 2021 16:47
by AR Coding
Thanks, Squashman. this is my version for my batch file:
Code: Select all
@echo off &setlocal EnableDelayedExpansion
set "dt1=Fri, 27 Aug 2021 12:37:30 GMT"
set "dt2=Fri, 27 Aug 2021 12:37:31 GMT"
for %%i in ("Jan=01" "Feb=02" "Mar=03" "Apr=04" "May=05" "Jun=06" "Jul=07" "Aug=08" "Sep=09" "Oct=10" "Nov=11" "Dec=12") do set %%i
for /f "tokens=2-7 delims=: " %%i in ("%dt1%") do set "timestamp1=%%k!%%j!%%i%%l%%m%%n"
for /f "tokens=2-7 delims=: " %%i in ("%dt2%") do set "timestamp2=%%k!%%j!%%i%%l%%m%%n"
echo %%timestamp1%% == %timestamp1%
echo %%timestamp2%% == %timestamp2%
if %timestamp1% GTR %timestamp2% (echo(%%timestamp1%% is greater) else (echo(%%timestamp2%% is greater)
if %timestamp1% EQU %timestamp2% echo Timestamps are equal
pause
Re: Date/Time Comparison
Posted: 26 Dec 2021 19:08
by Squashman
AR Coding wrote: ↑26 Dec 2021 16:47
Thanks, Squashman.
Re: Date/Time Comparison
Posted: 26 Dec 2021 21:19
by AR Coding
OMG! I did not realize, that what a mistake! @aGerman, I am so sorry! Thank you so much for your answer!
Re: Date/Time Comparison
Posted: 27 Dec 2021 07:13
by aGerman
I don't mind