Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Date manipulation  (Read 37235 times)

0 Members and 1 Guest are viewing this topic.

Dusty

    Topic Starter


    Egghead

  • I could if she would, but she won't so I don't.
  • Thanked: 75
  • Experience: Beginner
  • OS: Windows XP
Date manipulation
« on: December 07, 2008, 11:50:33 PM »
Manipulating dates in the Windows XP Command window.
The ability to manipulate dates in batch scripting is very limited.  Day, Month and Year can be extracted from either the 'date /t' command or the %date% environment variable only if the date format is known.  Returning dates in advance of, or prior to, today's date requires many lines of coding to check for such things as a new month, new year, leap year etc.

The following two codings will extract day date month and year from the date format day mm/dd/yyyy.


Code: [Select]
@echo off
cls

FOR /f "tokens=1-4 delims=/ " %%a in ('date /t') do (
set day=%%a
set mm=%%b
set dd=%%c
set yy=%%d
)

echo %day% %dd% %mm% %yy%

Code: [Select]
@echo off
cls

set day=%date:~0,3%
set mm=%date:~4,2%
set dd=%date:~7,2%
set yy=%date:~-4%

echo %day% %dd% %mm% %yy%



Returning dates in advance of, or prior to, today's date.
If one wanted to use a date which was 3, 30 or even 300 days ago or hence it is best achieved using a scripting language other than batch script.

Visual Basic Scripting language (VBS) is a built-in feature of Windows XP so for those who do not like to install programs/routines on their pc the following code gives the opportunity to manipulate dates to suit individual requirements.  (Only one small temporary file is created in the %Temp% folder and is deleted when no longer required).  VBS is used in conjunction with batch scripting to extract/display the various components of the date.  The code will be successful with any date format.

Code: [Select]
:: Acknowledgement.  Dias de verano for his input to set up the .vbs file.

:: Syntax: filename only - set/display today's date.
::         filename -n   - set/display today's date minus n days.
::         filename +n   - set/display today's date plus  n days.

@echo off
cls

:: Check for incorrect commandline entry..
set a=%1

if "%a%"=="" goto start
if "%a:~0,1%"=="+" goto start
if "%a:~0,1%"=="-" goto start

echo.&echo.
echo                     Incorrect entry - run aborted
echo.
echo                     Press any key to continue....
pause > nul
cls & exit/b

:: Create/run temporary .vbs file (extracts date components).
:start
set vb=%temp%\newdate.vbs
(
echo Newdate = (Date(^)%1^)
echo Yyyy = DatePart("YYYY", Newdate^)
echo   Mm = DatePart("M"   , Newdate^)
echo   Dd = DatePart("D"   , Newdate^)
echo   Wd = DatePart("WW"  , Newdate^)
echo   Wn = DatePart("Y"   , Newdate^)
echo   Ww = datepart("W"   , Newdate^)

echo Wscript.Echo Yyyy^&" "^&Mm^&" "^&Dd^&" "^&Wd^&" "^&Ww^&" "^&Wn
)>>%vb%

FOR /F "tokens=1-6 delims= " %%A in ('cscript //nologo %vb%') do (
        set Yyyy=%%A
        set Mm=%%B
        set Dd=%%C
        set Week#=%%D
        set Weekday#=%%E
        set Day#=%%F
)

del %vb%
::                    [Environment Variables are:
::                     %Yyyy%     Year in the format yyyy
::                     %Mm%       Month in the format m or mm
::                     %Dd%       Number of the day in month in the format d or dd (range 1 thru'31)
::                     %Week#%    Week number in year in the format w or ww (range 1 thru' 52)
::                     %Weekday#% Day number in week in the format w
::                                     (range 1 thru' 7, day#1 is Sunday)
::                     %Day#%     day number in the year in the format d thru ddd
::                                     (range 1 thru' 366)]

set days=
if not "%1"=="" set days=days
if %Mm% lss 10 set Mm=0%Mm%
if %Dd% lss 10 set Dd=0%Dd%

:: The following code serves to demonstrate just two of the formats which can be displayed.

echo.&echo.&echo.&echo.&echo.&echo.
echo            Date to be displayed is today %1 %days%
echo.
echo            Example of date in yyyy/mm/dd format:   %Yyyy%/%Mm%/%Dd%
echo               "    "   "    " mmddyyyy     "   :   %Mm%%Dd%%Yyyy%
echo.&echo.&echo.
echo            Press any key to continue...
pause > nul
cls & exit /b





For those who have no objection to installing a small utility in order to manipulate dates please download DOFF.ZIP from http://www.jfitz.com/dos/index.html and unzip it into your Path - the code below will be successful with any date format.

Code: [Select]
@echo off
cls

:: Syntax: filename only - set/display today's date.
::         filename -n   - set/display today's date minus n days.
::         filename +n   - set/display today's date plus  n days.

for /f "tokens=1-3 delims=/ " %%a in ('doff mm/dd/yyyy %1') do (
    set mm=%%a
    set dd=%%b
    set yyyy=%%c
)

:: The following code serves to demonstrate just two of the formats which can be displayed.

set days=
if not "%1"=="" set days=days

echo.&echo.&echo.&echo.&echo.&echo.
echo Date to be displayed is today %1 %days%
echo.
echo Date in the format dd mm yyyy           %dd% %mm% %yyyy%
echo.&echo.
echo Date in the format yyyy/mm/dd           %yyyy%/%mm%/%dd%


To view some of the capabilities of Doff.exe after unzipping it into your Path, at the Command Prompt enter:

Doff dd                        returns the day only
Doff mm                       returns the month only
Doff yy                         returns the year only e.g. 08
Doff yyyy                      returns the year only e.g. 2008
Doff ddmmyy                returns the day month and year e.g. 231008
Doff mmddyyyy             returns the month day and year e.g. 10312008
Doff ddmmyyyy -30       returns today's date minus 30 days
Doff mmddyyyy +300    returns today's date plus 300 days

for /f %%I in ('doff yyyymmdd +10') do echo %%I

will return todays date +10 days in the format yyyymmdd

Note that throughout the above codings syntax checking is minimal or non-existent.  If the syntax of any command-line entry is incorrect the results are unpredictable.

Good luck.
« Last Edit: June 15, 2009, 08:22:34 PM by Dusty »
One good deed is worth more than a year of good intentions.