Can I calculate a date?
Moderator: DosItHelp
Can I calculate a date?
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.
)
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.
)
dzo,
Sure you can do this:
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.
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.
Calculate with dates
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
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
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.
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.
A little bit faster
Hi,
your solution
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.
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
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
Any other way without using jdate/jdate2date ?
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
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.
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