Page 1 of 1

Can I calculate a date?

Posted: 07 Jan 2008 09:21
by dzo
Is it possible in a batchfile to calculate a date as today+3weeks? Or today + 2 days?
I have already a syntax that puts the current date in an variable, but I think I need a kind of calendar function to manipulate.
I need this 'date' to be a part of a copy command.

Does anyone have a solution?

Thanks!

DZo


(my syntax is: For /f "tokens=2-4 delims=- " %%a in ('date /t') do (set date=%tot%%%b%%a), whereafter I can use %date%.txt.

)

Posted: 07 Jan 2008 20:58
by DosItHelp
dzo,

Sure you can do this:

@echo off
call:jdate JD "%DATE%"
set /a JD+=3*7
call:jdate2date JD YYYY MM DD
set filename=%MM%%DD%.txt

echo.%filename%

GOTO:EOF
REM --- This is the end of the main script, functions follow below ---

rem Get the two functions from here:
rem http://www.dostips.com/DtCodeCmdLib.php#jdate
rem http://www.dostips.com/DtCodeCmdLib.php#jdate2date



The DATE variable is a system environment variable you can use instead of the date /t command.
The :jdate function converts Gregorian date to Julian days and sets the JD variable for you.
Then you can add 3 weeks (3x7 days) to the Julian days
The :jdate2date function finally converts the Julian days back to Gregorian days and stores the year, month and day in the YYYY, MM and DD variable.

Let me know if it works. :wink:

Calculate with dates

Posted: 08 Jan 2008 05:52
by dzo
Hello DosItHelp,

I understand your suggestion, but it does not work totally well. I have set the echo on and have seen what happens:
The first function returns a Julian date of 2454466 for 8 Jan 2008, whereas the real Julian date should be 2454473.5. If I call the second function with argument 2454474, I get the correct result.

My environment is: XP professional in Ducth language, so I get the date as di 08-01-2008, but the parsing is done correctly.

Do you have an other suggestion? Would be welcome!

Thanks,

DZo

Posted: 08 Jan 2008 20:42
by DosItHelp
DZo,

You found a bug, thank you!! The functions :jdate and :date2jdate have been corrected now.

The problem was that the set /a ... command treads a number with leading zeros as octal number. 08-01-2008 caused trouble because 08 resulted in a invalid number and the command processor secretly assumed zero.

Both day and month could cause this problem.
The fix for local variable yy, mm and dd is:

Step 1: ensure day and month are two characters long and year 4
How:Prefix plenty leading zeros. From the resulting string take the last 2 or 4 characters
Step 2: prefix the strings with a 1, and use it in the "set /a" command
Now its decimal for sure, just 100 or 10000 to much
Step 3: subtract 100 from day and month, subtract 10000 from the year.

Like this:
set /a "yy=10000%yy%, mm=10000%mm%, dd=10000%dd%"
set /a "yy=1%yy:~-4%-10000,mm=1%mm:~-2%-100,dd=1%dd:~-2%-100"

The fix is uploaded, let me know how it goes. :wink:

A little bit faster

Posted: 10 Jan 2008 07:38
by jeb
Hi,

your solution

Code: Select all

set /a "yy=10000%yy%, mm=10000%mm%, dd=10000%dd%" 
set /a "yy=1%yy:~-4%-10000,mm=1%mm:~-2%-100,dd=1%dd:~-2%-100"


My suggestion could build with one line, more compact and faster.
Tested on my system 40000 times, it is two times faster, 8 instead of 16seconds.

Code: Select all

set /a "yy=10000%yy% %% 10000,mm=100%mm% %% 100,dd=100%dd% %% 100"



Step1: ensure day and month are greater than 100 or equal (if they are empty), do the same with year but with 10000
Step2: Modulo with 100 (or 10000 for years)

hope it is useful
Jan Erik

Great!

Posted: 10 Jan 2008 09:44
by dzo
Thanks everybody,

I have a working solution now.

I find it amazing that these advanced features were there all time.

DZo

Posted: 10 Jan 2008 18:48
by DosItHelp
Thanks Jan, I like it. Your suggestion has been implemented :D
Also thanks for testing :!:

Any other way without using jdate/jdate2date ?

Posted: 25 May 2008 09:22
by chcfrank
What I want to do is to return the date 30(or any number but usually less than 90) days from today(or any specified date string like 03/07/2008).
I am thinking whether to use VBScript DateAdd function but I would like to use complete DOS Batch command(if the code is simple/short enough) if possible. The idea so far is to use the idea I have about calculating day of the year(in the other post) and recursive subtracting one month worth of days at a time.

Any ideas will be greatly appreciated.

-Frank

DosItHelp wrote:dzo,

Sure you can do this:

@echo off
call:jdate JD "%DATE%"
set /a JD+=3*7
call:jdate2date JD YYYY MM DD
set filename=%MM%%DD%.txt

echo.%filename%

GOTO:EOF
REM --- This is the end of the main script, functions follow below ---

rem Get the two functions from here:
rem http://www.dostips.com/DtCodeCmdLib.php#jdate
rem http://www.dostips.com/DtCodeCmdLib.php#jdate2date



The DATE variable is a system environment variable you can use instead of the date /t command.
The :jdate function converts Gregorian date to Julian days and sets the JD variable for you.
Then you can add 3 weeks (3x7 days) to the Julian days
The :jdate2date function finally converts the Julian days back to Gregorian days and stores the year, month and day in the YYYY, MM and DD variable.

Let me know if it works. :wink:

Posted: 30 Jun 2008 04:31
by Thebetr1
Hey i dont know weather this helps or not but ill give it to you anyway


Code: Select all



@ECHO OFF

:: --- Change thease to then date you wish --- ::
set year=2008
set month=12
set day=31



set d=%date:~4,10%
set a=%d:~0,2%
set b=%d:~3,2%
set c=%d:~6,4%
if "%c%" GEQ "%year%" (
if "%b%" GEQ "%month%" (
if "%a%" GEQ "%day%" (

start a.exe


)
)
)

exit