S
S
Seli_one2021-10-26 17:59:52
cmd/bat
Seli_one, 2021-10-26 17:59:52

How to compare two dates in dat?

Hello colleagues.
There is a task of comparison of two dates.
I get the first date like this:
set now=%date%
I get the second one like this:

FOR /F "tokens=* USEBACKQ" %%a IN (`net user /DOMAIN %username% ^| findstr /C:"Password expires"`) DO (set p=%%a)
set pass=%p:~29,-9%

Next, I need to compare these dates.
And if the difference between the dates is some n days, perform some actions.
Tell me, maybe someone knows how to work with dates.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
res2001, 2021-10-26
@res2001

Batniks do not know how to work with dates. All variables are strings.
Yes, it set /acan perform integer arithmetic, but the result is still a string.
You need to convert the dates to the number of days since the beginning of the epoch, and then they can be compared using standard means.
The easiest way is to use more advanced tools for these purposes that can work with dates, for example, powershel, JS, VBS - these scripts can be called from the batch file if desired.
But if you really want to, you can count the batch file. The calculation algorithm is on the wiki. The code can be pulled from here - https://www.robvanderwoude.com/files/datediff_nt.txt

O
Olgeir, 2021-10-28
@Olgeir

see https://olegon.ru/showthread.php?t=16437 datemath.bat library

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question