Date/Time Comparison

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
AR Coding
Posts: 53
Joined: 02 May 2021 21:16

Date/Time Comparison

#1 Post by AR Coding » 25 Dec 2021 22:43

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:

Code: Select all

Fri, 27 Aug 2021 12:37:30 GMT
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

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Date/Time Comparison

#2 Post by aGerman » 26 Dec 2021 15:36

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

AR Coding
Posts: 53
Joined: 02 May 2021 21:16

Re: Date/Time Comparison

#3 Post by AR Coding » 26 Dec 2021 16:47

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

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Date/Time Comparison

#4 Post by Squashman » 26 Dec 2021 19:08

AR Coding wrote:
26 Dec 2021 16:47
Thanks, Squashman.
:roll:

AR Coding
Posts: 53
Joined: 02 May 2021 21:16

Re: Date/Time Comparison

#5 Post by AR Coding » 26 Dec 2021 21:19

OMG! I did not realize, that what a mistake! @aGerman, I am so sorry! Thank you so much for your answer!

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Date/Time Comparison

#6 Post by aGerman » 27 Dec 2021 07:13

I don't mind :lol:

Post Reply