Computing.Net > Forums > Windows XP > How to add dates in batch script

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

How to add dates in batch script

Reply to Message Icon

Name: Matthew (by PebbleMatt)
Date: November 14, 2005 at 02:50:07 Pacific
OS: Windows XP Pro SP2
CPU/Ram: P4
Comment:

Hi,

I am trying to figure out how to do arithmatic with dates.

If I want to find out what the date will be 2 days from now, I've tried
set /a enable_date=%date% + 3
it treats it as a string and echoing the variable just displays "14/11/2005+3"

I have tried subracting dates, so for example
set /a enable_date=20/11/2005 - %date%
but echong the variable in that situation just gives "0" (its the 14/11/2005 today so it should really return 6 if it was working)

Anyone have any ideas on how I can do this?

I've considered splitting the date up into DD MM YYYY and then adding a number to the DD and putting it all back together but that seems a bit complicated as I'd then have to take into account incrementing the month if the day reaches the end of that particular month, and then the year if its the end of december.

Thanks for any help




Sponsored Link
Ads by Google

Response Number 1
Name: IVO
Date: November 14, 2005 at 03:27:13 Pacific
Reply:

Matthew, you just have answered by yourself: date math is cumbersome and absolutely no friendly to perform using strictly the batch language. There is no builtin functions to handle that issue, so better you try another scripting tool (e.g. Perl), code your own utility in the language you like or look for some tool on the net.


0

Response Number 2
Name: Mechanix2Go
Date: November 14, 2005 at 03:40:14 Pacific
Reply:

Hi Matthew & IVO,

Yeah, as Dr. McCoy always tell Captain Kirk, it's worse than that, Jim.

Seems dtech10 has worked the batch issue:

http://computing.net/programming/wwwboard/forum/13207.html

I'm working on a solution in C.


If at first you don't succeed, you're about average.

M2


0

Response Number 3
Name: dtech10
Date: November 14, 2005 at 06:52:45 Pacific
Reply:

Hi
This help I'm assuming an English Date.
Usage Prog.bat (Number Of Days)
ie Prog 14 ;Date in 14 days Time
ie Prog -12 ;Date 12 Days Ago
Is this what you require or the number of days between dates.

------------
@echo off
for /f "tokens=1-4 delims=/ " %%a in ('date /t') do (
set dd=%%a
set mm=%%b
set yy=%%c
)
set mo=%mm%
set yr=%yy%
if %mm% LSS 3 (
set /a mo=%mo%+12
set /a yr=%yr%-1
)
set /a mo=%mo%+1
set /a a=%yr%/100
set /a b=2-%a%+%a%/4
set /a jd=%yr%*36525/100+%mo%*306001/10000+%dd%+%b%-694084

echo Today=%dd%/%mm%/%yy%

if %dd% LSS 10 set dd=0%dd%
if %mm% LSS 10 set mm=0%mm%

set /a jd=%jd%+%1
set /a yy=%jd%*100/36525
set /a dd=%jd%-%yy%*36525/100
set /a mm=%dd%*10/306
set /a dd=%dd%-(%mm%*306+5)/10
if %dd%==0 (
set dd=31
set /a mm=%mm%-1
if %mm%==0 set dd=29
)
set /a mm=%mm%+3
if %mm% GTR 12 (
set /a mm=%mm%-12
set /a yy=%yy%+1
)
set /a yy=%yy%+1900
if %dd% LSS 10 set dd=0%dd%
if %mm% LSS 10 set mm=0%mm%
set NDate=%dd%/%mm%/%yy%
echo NDate=%NDate%
for %%a in (a b dd mm yy jd mo yr) do set %%a=


0

Response Number 4
Name: Mechanix2Go
Date: November 14, 2005 at 07:20:10 Pacific
Reply:

Hi Matthew & IVO & dtech10,

Take a look at my new post in programming.

Not exactly math, but a small step in finding 'older' files.

older


If at first you don't succeed, you're about average.

M2


0

Response Number 5
Name: Matthew (by PebbleMatt)
Date: November 14, 2005 at 13:17:03 Pacific
Reply:

Thanks for the help,

dtech10, thats what I needed cheers. I'll need to make a small change though as the date will not always be the current date, so need to be able to be able to type in another date at the prompt. Shouldn't be too hard to that I think.

Thanks again everyone.


0

Related Posts

See More



Response Number 6
Name: dtech10
Date: November 14, 2005 at 16:17:53 Pacific
Reply:

This Help
Usage: Prog.bat 15/11/2005 4
Add 4 Days to 15th Nov 2005
@echo off
echo %1 > ~Date.txt
for /f "tokens=1-4 delims=/ " %%a in (~Date.txt) do (
set dd=%%a
set mm=%%b
set yy=%%c
)
set mo=%mm%
set yr=%yy%
if %mm% LSS 3 (
set /a mo=%mo%+12
set /a yr=%yr%-1
)
set /a mo=%mo%+1
set /a a=%yr%/100
set /a b=2-%a%+%a%/4
set /a jd=%yr%*36525/100+%mo%*306001/10000+%dd%+%b%-694084

echo Today=%dd%/%mm%/%yy%

if %dd% LSS 10 set dd=0%dd%
if %mm% LSS 10 set mm=0%mm%

set /a jd=%jd%+%2
set /a yy=%jd%*100/36525
set /a dd=%jd%-%yy%*36525/100
set /a mm=%dd%*10/306
set /a dd=%dd%-(%mm%*306+5)/10
if %dd%==0 (
set dd=31
set /a mm=%mm%-1
if %mm%==0 set dd=29
)
set /a mm=%mm%+3
if %mm% GTR 12 (
set /a mm=%mm%-12
set /a yy=%yy%+1
)
set /a yy=%yy%+1900
if %dd% LSS 10 set dd=0%dd%
if %mm% LSS 10 set mm=0%mm%
set NDate=%dd%/%mm%/%yy%
echo NDate=%NDate%
for %%a in (a b dd mm yy jd mo yr) do set %%a=
del ~Date.txt



0

Response Number 7
Name: Matthew (by PebbleMatt)
Date: December 9, 2005 at 03:50:31 Pacific
Reply:

Thanks dtech10,

just a note though, I noticed yesterday and today, the script does not appear to work too well with days less than 10 (so 1st to 9th), so todays date for example (09/12/2005) ended up giving the error:
Invalid number. Numeric constants are either decimal (17),
hexadecimal (0x11), or octal (021).

its this line which caused the problem once it gets to the adding the %dd%:
jd=%yr%*36525/100+%mo%*306001/10000+%dd%+%b%-694084

I've added the following code before that arithmatic line and it works again:
if %dd:~0,1% EQU 0 (
set dd=%dd:~1,1%
)

Could you please explain to me what the various numbers are actually for? Just would like to know what they are referring to or how you came to those values
jd=%yr%*36525/100+%mo%*306001/10000+%dd%+%b%-694084

thanks again


0

Response Number 8
Name: Mechanix2Go
Date: December 9, 2005 at 04:45:14 Pacific
Reply:

Hi Matthew,

I won't butt into dtech10's cade.

Are you asking about the "octal gotcha"?

For some reason, M$ decided that a number starting with a 0 is octal.

Your stripping of the leading zero will work.

Also this slick trick from another guy's code:

set /a x=108-100

or

set /a x=1%dd%-100


If at first you don't succeed, you're about average.

M2Go


0

Response Number 9
Name: Matthew (by PebbleMatt)
Date: December 9, 2005 at 04:53:21 Pacific
Reply:

neat trick, thanks


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Windows XP Forum Home


Sponsored links

Ads by Google


Results for: How to add dates in batch script

How to add icons in Desktop Status www.computing.net/answers/windows-xp/how-to-add-icons-in-desktop-status-/148477.html

how to compare two dates in bat DOS www.computing.net/answers/windows-xp/how-to-compare-two-dates-in-bat-dos/162686.html

Batch script to rename files www.computing.net/answers/windows-xp/batch-script-to-rename-files/173968.html