Date variable in a command file

  • Thread starter Thread starter JDR
  • Start date Start date
J

JDR

I have a cmd line that reads

set vardate=%date%

I need to assign the vardate a value of yesterday's date.

Is there a way to assign the current date -1?

Thanks
 
JDR said:
I have a cmd line that reads

set vardate=%date%

I need to assign the vardate a value of yesterday's date.

Is there a way to assign the current date -1?

Thanks

Try Timo Salmi's FAQ in alt.msdos.batch.nt; last posted today - item 6.
 
I'm going to be stupid but where is alt.msdos.batch.nt?

Thanks

It's another news group - in the same way as you've posted this message to
microsoft.public.win2000.cmdprompt.admin, try alt.msdos.batch.nt - or
alt.msdos.batch - which carries the same thread (some news servers don't
carry ...nt; most would carry ...batch)

Or Google for "alt.msdos.batch.nt" or "timo salmi batch faq"
 
I have a cmd line that reads

set vardate=%date%

I need to assign the vardate a value of yesterday's date.

Is there a way to assign the current date -1?

:: Date foward & backward
@echo off
:: from code by Phil Robyn
setlocal
if [%1]==[] (
echo to get todays date use
echo call "%~n0" today 0
echo.
echo to get yesterdays date use
echo call "%~n0" today -1
echo.
echo to get the date 25 days ago:
echo call "%~n0" today -25
echo.
echo to get the date 1250 days in the future
echo call "%~n0" today +1250
goto :EOF)

set date1=%1
set qty=%2
if /i "%date1%" EQU "TODAY" (
set date1=now
) else (
set date1="%date1%"
)
echo >"%temp%\%~n0.vbs" s=DateAdd("d",%qty%,%date1%)
echo>>"%temp%\%~n0.vbs" WScript.Echo year(s)^&_
echo>>"%temp%\%~n0.vbs" right(100+month(s),2)^&_
echo>>"%temp%\%~n0.vbs" right(100+day(s),2)
for /f %%a in (
'cscript //nologo "%temp%\%~n0.vbs"') do set result=%%a
del "%temp%\%~n0.vbs"
endlocal& set day=%result:~0,4%-%result:~4,2%-%result:~6,2%
echo %%day%% is set to "%day%" (without the quotes)
 
Try Timo Salmi's FAQ in alt.msdos.batch.nt; last posted today - item 6.

Below is a copy of the posting in case the OP lacks access. The direct
link to the said item #6 is

6} How does one get yesterday's date?
http://www.netikka.net/tsneti/info/tscmd006.htm

<snip>
Dear new alt.msdos.batch and alt.msdos.batch.nt readers,

Before posing a question you might first wish to take a look at the
frequently asked questions in these newsgroups.

An online version of Useful NT/2000/XP batch file tricks and tips
http://www.netikka.net/tsneti/info/tscmd.htm

A FAQ collection of NT/2000/XP batch file tricks and tips
http://garbo.uwasa.fi/pub/pc/link/tscmd.zip

A FAQ collection of MSDOS/Win9x/ME etc batch files tricks and tips
http://garbo.uwasa.fi/pub/pc/link/tsbat.zip

For further reading: Windows scripting and programming links
http://lipas.uwasa.fi/~ts/http/http2.html#cmdscript

Also: MS-DOS+Win../95/98/Me batch programming links
http://lipas.uwasa.fi/~ts/http/http2.html#batch

The main scope of these batch file related newsgroups are
alt.msdos.batch :for MS-DOS+Win../95/98/Me batch scripts
alt.msdos.batch.nt :for NT/2000/XP/.. batch scripts

Please define your OS when posing a batch/script question.
</snip>

All the best, Timo
 
I have a cmd line that reads
set vardate=%date%
I need to assign the vardate a value of yesterday's date.
Is there a way to assign the current date -1?

:: Date foward & backward
@echo off
:: from code by Phil Robyn
setlocal
if [%1]==[] (
  echo to get todays date use
  echo call "%~n0" today 0
  echo.
  echo to get yesterdays date use
  echo call "%~n0" today -1
  echo.
  echo to get the date 25 days ago:
  echo call "%~n0" today -25
  echo.
  echo to get the date 1250 days in the future
  echo call "%~n0" today +1250
  goto :EOF)

set date1=%1
set qty=%2
if /i "%date1%" EQU "TODAY" (
 set date1=now
) else (
 set date1="%date1%"
)
echo >"%temp%\%~n0.vbs" s=DateAdd("d",%qty%,%date1%)
echo>>"%temp%\%~n0.vbs" WScript.Echo year(s)^&_
echo>>"%temp%\%~n0.vbs"         right(100+month(s),2)^&_
echo>>"%temp%\%~n0.vbs"         right(100+day(s),2)
for /f %%a in (
  'cscript //nologo "%temp%\%~n0.vbs"') do set result=%%a
del "%temp%\%~n0.vbs"
endlocal& set day=%result:~0,4%-%result:~4,2%-%result:~6,2%
echo %%day%% is set to "%day%" (without the quotes)

It seems to me to be easier to use a batch/WSH hybrid here, as in ...

::RunWSH.CMD
@echo off
if '%1==' goto :EOF
if /i '%1=='date %_then_% (
%0 "year(now)&right(chr(48)&month(now),2)&right(chr(48)&day(now),
2)%2"
)
echo wsh.echo "res="^&eval("%~1")> %temp%\tmp.vbs
for /f "delims=" %%a in ('cscript //nologo %temp%\tmp.vbs') do set %
%a
del %temp%\tmp.vbs
% For demonstration % echo Res = %Res%

This takes up to two arguments. The first can be an expression for
evaluation, or the word DATE (case insensitive) and the second
[optional] argument can be a continuation of the evaluation or a
number to add (+/-n) to the DATE. The expressions can be enclosed
with double quotes, if needed to handle included delimiters, like
commas or spaces. However, the procedure as written cannot handle
string (text) expressions.

The procedure stores its return value in the environment variable
RES. The last line is intended as an illustration, only. It is best
to remove it or comment it out for production use.

In use, the current question can be answered in several ways,
depending on the desired format of the result ...

C:\>RunWSH date -1
Res = 20090215

or

C:\>RunWSH date-1
Res = 2/15/2009

or

C:\>RunWSH now-1
Res = 2/15/2009 8:14:09 AM

I suspect the first version is the most helpful.

The procedure is not limited to date manipulation, however. For
example, the issue of handling decimal math ...

C:\>RunWSH 2/3
Res = 0.666666666666667

etc. Anything that can be expressed as a valid VBS mathematical
expression. Values stored in environment variables can be used, as
with any batch procedure. Knowledge of VBS IS needed to use
complicated expressions or intrinsic functions, but not to do
straightforward math. Such math expressions are almost universally as
you might expect, simply the numbers with the operators between.

Tom Lavedas
***********
http://there.is.no.more/tglbatch/
 
Excellent!

Thanks for every ones input!

Tom Lavedas said:
I have a cmd line that reads
set vardate=%date%
I need to assign the vardate a value of yesterday's date.
Is there a way to assign the current date -1?

:: Date foward & backward
@echo off
:: from code by Phil Robyn
setlocal
if [%1]==[] (
echo to get todays date use
echo call "%~n0" today 0
echo.
echo to get yesterdays date use
echo call "%~n0" today -1
echo.
echo to get the date 25 days ago:
echo call "%~n0" today -25
echo.
echo to get the date 1250 days in the future
echo call "%~n0" today +1250
goto :EOF)

set date1=%1
set qty=%2
if /i "%date1%" EQU "TODAY" (
set date1=now
) else (
set date1="%date1%"
)
echo >"%temp%\%~n0.vbs" s=DateAdd("d",%qty%,%date1%)
echo>>"%temp%\%~n0.vbs" WScript.Echo year(s)^&_
echo>>"%temp%\%~n0.vbs" right(100+month(s),2)^&_
echo>>"%temp%\%~n0.vbs" right(100+day(s),2)
for /f %%a in (
'cscript //nologo "%temp%\%~n0.vbs"') do set result=%%a
del "%temp%\%~n0.vbs"
endlocal& set day=%result:~0,4%-%result:~4,2%-%result:~6,2%
echo %%day%% is set to "%day%" (without the quotes)

It seems to me to be easier to use a batch/WSH hybrid here, as in ...

::RunWSH.CMD
@echo off
if '%1==' goto :EOF
if /i '%1=='date %_then_% (
%0 "year(now)&right(chr(48)&month(now),2)&right(chr(48)&day(now),
2)%2"
)
echo wsh.echo "res="^&eval("%~1")> %temp%\tmp.vbs
for /f "delims=" %%a in ('cscript //nologo %temp%\tmp.vbs') do set %
%a
del %temp%\tmp.vbs
% For demonstration % echo Res = %Res%

This takes up to two arguments. The first can be an expression for
evaluation, or the word DATE (case insensitive) and the second
[optional] argument can be a continuation of the evaluation or a
number to add (+/-n) to the DATE. The expressions can be enclosed
with double quotes, if needed to handle included delimiters, like
commas or spaces. However, the procedure as written cannot handle
string (text) expressions.

The procedure stores its return value in the environment variable
RES. The last line is intended as an illustration, only. It is best
to remove it or comment it out for production use.

In use, the current question can be answered in several ways,
depending on the desired format of the result ...

C:\>RunWSH date -1
Res = 20090215

or

C:\>RunWSH date-1
Res = 2/15/2009

or

C:\>RunWSH now-1
Res = 2/15/2009 8:14:09 AM

I suspect the first version is the most helpful.

The procedure is not limited to date manipulation, however. For
example, the issue of handling decimal math ...

C:\>RunWSH 2/3
Res = 0.666666666666667

etc. Anything that can be expressed as a valid VBS mathematical
expression. Values stored in environment variables can be used, as
with any batch procedure. Knowledge of VBS IS needed to use
complicated expressions or intrinsic functions, but not to do
straightforward math. Such math expressions are almost universally as
you might expect, simply the numbers with the operators between.

Tom Lavedas
***********
http://there.is.no.more/tglbatch/
 
Back
Top