Computer Hope


Computer Hope Forum Welcome, Guest. Please login or register.
February 09, 2010, 02:03:34 PM
Home Help Staff Chat Login Register
News: Come introduce yourself and meet the other forum members.

Computer Hope - Computer Help Forums  >>  Other  >>  FAQ solutions database  >>  Software  >>  MS-DOS  >>  Topic: Date manipulation 0 Members and 1 Guest are viewing this topic. « previous next »
Pages: [1] - (Bottom) Print
Author Topic: Date manipulation  (Read 4129 times)
Dusty
Topic Starter
Egghead
*
Posts: 3141

Thanked: 44
OS: Windows XP
Experience: Beginner



Country music Rocks.


« 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:
@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:
@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:
:: 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:
@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 » Logged

Light travels faster than sound.  This is why some people appear bright until you hear them speak.
Pages: [1] - (Top) Print 
Computer Hope - Computer Help Forums  >>  Other  >>  FAQ solutions database  >>  Software  >>  MS-DOS  >>  Topic: Date manipulation « previous next »
Jump to:  


Login with username, password and session length

Old Forum Search | Forum Rules
Copyright 1998-2010 by Computer Hope (tm). All rights reserved
Powered by SMF 1.1.8 | SMF © 2006-2008, Simple Machines LLC
Page created in 5.145 seconds with 20 queries.